key-value-observing

Adding observer for KVO without pointers using Swift

﹥>﹥吖頭↗ 提交于 2019-11-26 19:36:59
问题 In Objective-C, I would normally use something like this: static NSString *kViewTransformChanged = @"view transform changed"; // or static const void *kViewTransformChanged = &kViewTransformChanged; [clearContentView addObserver:self forKeyPath:@"transform" options:NSKeyValueObservingOptionNew context:&kViewTransformChanged]; I have two overloaded methods to choose from to add an observer for KVO with the only difference being the context argument: clearContentView.addObserver(observer:

Key Value Observing with an NSArray

不想你离开。 提交于 2019-11-26 19:31:20
问题 I've looked on SO for examples of using Key Value Observing with an NSArray (or NSMutableArray ) and apparently you need to use an NSArrayController (which unlike KVO I'm not familiar with), but I haven't found concrete examples of how to do this. Can anyone explain with some sample code? For instance, if I have a GameModel which represents its player names with an NSArray (playerNameArray) of NSStrings . I want to observe those strings (the view controller observes the model's data) to

Observing an NSMutableArray for insertion/removal

前提是你 提交于 2019-11-26 16:59:59
A class has a property (and instance var) of type NSMutableArray with synthesized accessors (via @property ). If you observe this array using: [myObj addObserver:self forKeyPath:@"theArray" options:0 context:NULL]; And then insert an object in the array like this: [myObj.theArray addObject:NSString.string]; An observeValueForKeyPath... notification is not sent. However, the following does send the proper notification: [[myObj mutableArrayValueForKey:@"theArray"] addObject:NSString.string]; This is because mutableArrayValueForKey returns a proxy object that takes care of notifying observers.

Why would you use an ivar?

99封情书 提交于 2019-11-26 11:30:25
I usually see this question asked the other way, such as Must every ivar be a property? (and I like bbum's answer to this Q). I use properties almost exclusively in my code. Every so often, however, I work with a contractor who has been developing on iOS for a long time and is a traditional game programmer. He writes code that declares almost no properties whatsoever and leans on ivars. I assume he does this because 1.) he's used to it since properties didn't always exist until Objective C 2.0 (Oct '07) and 2.) for the minimal performance gain of not going through a getter / setter. While he

iOS: How do I know if a property is KVO-compliant?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 11:13:52
问题 In the Key-Value Observing Programming Guide, the section Registering for Key-Value Observing says \"Typically properties in Apple-supplied frameworks are only KVO-compliant if they are documented as such.\" But, I haven\'t found any properties in the documentation that are documented as KVO-compliant. Would you please point me to some? Specifically, I would like to know if the @property(nonatomic,retain) UIViewController *rootViewController of UIWindow is KVO-compliant. The reason is that I\

KVO and ARC how to removeObserver

僤鯓⒐⒋嵵緔 提交于 2019-11-26 10:27:13
问题 How do you remove an observer from an object under ARC? Do we just add the observer and forget about removing it? If we no longer manage memory manually where do we resign from observing? For example, on a view controller: [self.view addObserver:self forKeyPath:@\"self.frame\" options:NSKeyValueObservingOptionNew context:nil]; Previously, I would call removeObserver: in the view controller\'s dealloc method. 回答1: You still can implement -dealloc under ARC, which appears to be the appropriate

How can I do Key Value Observing and get a KVO callback on a UIView's frame?

偶尔善良 提交于 2019-11-26 10:16:51
I want to watch for changes in a UIView 's frame , bounds or center property. How can I use Key-Value Observing to achieve this? There are usually notifications or other observable events where KVO isn't supported. Even though the docs says 'no' , it is ostensibly safe to observe the CALayer backing the UIView. Observing the CALayer works in practice because of its extensive use of KVO and proper accessors (instead of ivar manipulation). It's not guaranteed to work going forward. Anyway, the view's frame is just the product of other properties. Therefore we need to observe those: [self.view

Is key-value observation (KVO) available in Swift?

五迷三道 提交于 2019-11-26 06:04:47
If so, are there any key differences that weren't otherwise present when using key-value observation in Objective-C? Yes and no. KVO works on NSObject subclasses much as it always has. It does not work for classes that don't subclass NSObject. Swift does not (currently at least) have its own native observation system. (See comments for how to expose other properties as ObjC so KVO works on them) See the Apple Documentation for a full example. You can use KVO in Swift, but only for dynamic properties of NSObject subclass. Consider that you wanted to observe the bar property of a Foo class. In

Observing an NSMutableArray for insertion/removal

限于喜欢 提交于 2019-11-26 05:00:03
问题 A class has a property (and instance var) of type NSMutableArray with synthesized accessors (via @property ). If you observe this array using: [myObj addObserver:self forKeyPath:@\"theArray\" options:0 context:NULL]; And then insert an object in the array like this: [myObj.theArray addObject:NSString.string]; An observeValueForKeyPath... notification is not sent. However, the following does send the proper notification: [[myObj mutableArrayValueForKey:@\"theArray\"] addObject:NSString.string]

Is key-value observation (KVO) available in Swift?

只愿长相守 提交于 2019-11-26 01:56:46
问题 If so, are there any key differences that weren\'t otherwise present when using key-value observation in Objective-C? 回答1: Yes and no. KVO works on NSObject subclasses much as it always has. It does not work for classes that don't subclass NSObject. Swift does not (currently at least) have its own native observation system. (See comments for how to expose other properties as ObjC so KVO works on them) See the Apple Documentation for a full example. 回答2: You can use KVO in Swift, but only for