key-value-observing

Key Value Observing with an NSArray

时光总嘲笑我的痴心妄想 提交于 2019-11-27 18:34:41
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 update various things in the view. How do I get notification that the player name array has changed? EDIT :

Receiving 2 KVO notifications for a single KVC change

僤鯓⒐⒋嵵緔 提交于 2019-11-27 17:28:23
问题 I'm using KVC/KVO to create a custom bindings implementation for a University project (it needs to be custom as I want to do things beyond what bindings can do, including running on iOS). I have a 'bindings controller' that registers for KVO notifications on a number of keys on an object (using addObserver:forKeyPath:options:context:) and I do receive notifications. However I am receiving two notifications for each change. I have an idea for a workaround, but I would rather work out why this

How can i tell if an object has a key value observer attached

五迷三道 提交于 2019-11-27 16:54:54
if you tell an objective c object to removeObservers: for a key path and that key path has not been registered, it cracks the sads. like - 'Cannot remove an observer for the key path "theKeyPath" from because it is not registered as an observer.' is there a way to determine if an object has a registered observer, so i can do this if (object has observer){ remove observer } else{ go on my merry way } Adam Put a try catch around your removeObserver call @try{ [someObject removeObserver:someObserver forKeyPath:somePath]; }@catch(id anException){ //do nothing, obviously it wasn't attached because

NSMutableDictionary KVO

只愿长相守 提交于 2019-11-27 16:51:17
问题 I'm trying to observe changes in dictionary using KVO. Example: dictionary = [NSMutableDictionary new]; [dictionary setObject:@"test1" forKey:@"key1"]; [dictionary setObject:@"test2" forKey:@"key2"]; [dictionary setObject:@"test3" forKey:@"key1"]; I'd love to be able to hook an observer for whenever a value is added to the dictionary. removed, or replaced (ie in the above cases, whenever any of the setObject methods are called) So in conclusion: I want a function to have - (void

Key-Value Observe in Swift not showing insertions and removals in arrays

旧巷老猫 提交于 2019-11-27 16:26:18
问题 I created a class which contains an array. I added an observer to that array in a view controller and performed some modifications to that array. The problem is that when I print the change dictionary returned by the observeValueForKeyPath() method I can only see changes of kind NSKeyValueChangeSetting. In other words, the method tells me the array has changed, provides me with the old and new arrays (containing all elements) but I would like to receive the information of which specific items

Checking if a value is changed using KVO in Swift 3

二次信任 提交于 2019-11-27 12:58:59
问题 I would like to know when a set of properties of a Swift object changes. Previously, I had implemented this in Objective-C, but I'm having some difficulty converting it to Swift. My previous Objective-C code is: - (void) observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context { if (![change[@"new"] isEqual:change[@"old"]]) [self edit]; } My first pass at a Swift solution was: override func observeValue(forKeyPath keyPath: String?, of

Key-Value-Observing a to-many relationship in Cocoa

馋奶兔 提交于 2019-11-27 11:51:28
I am trying to get key-value-observing to work for an NSMutableArray. Below is the .h file for MyObservee, the observed class: @interface MyObservee : NSObject { @private int someValue; @private NSMutableArray *someArray; } @property (readwrite,assign) int someValue; - (NSMutableArray *)someArray; @end The class MyObserver implements observeValueForKeyPath:ofObject:change:context:. Here is how I add the observer: MyObservee *moe = [[MyObservee alloc] init]; MyObserver *mobs = [[MyObserver alloc] init]; [moe addObserver:mobs forKeyPath:@"someArray" options:(NSKeyValueObservingOptionNew |

Why does NSOperation disable automatic key-value observing?

*爱你&永不变心* 提交于 2019-11-27 10:49:38
问题 When working with a custom NSOperation subclass I noticed that the automatic key-value observing is disabled by the [NSOperation automaticallyNotifiesObserversForKey] class method (which returns NO at least for some key paths). Because of that the code inside of NSOperation subclasses is littered by manual calls to willChangeValueForKey: and didChange… , as visible in many code samples on the web. Why does NSOperation do that? With automatic KVO support people could simply declare properties

How do I find all the property keys of a KVC compliant Objective-C object?

孤街浪徒 提交于 2019-11-27 10:01:04
问题 Is there a method that returns all the keys for an object conforming to the NSKeyValueCoding protocol? Something along the lines of [object getPropertyKeys] that would return an NSArray of NSString objects. It would work for any KVC-compliant object. Does such a method exist? I haven't found anything in searching the Apple docs so far. Thanks, G. 回答1: #import "objc/runtime.h" unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList([self class], &outCount); for(i = 0; i

KVO vs NSNotification vs protocol/delegates?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 09:35:46
问题 Though I have some idea which to use when but the exact usage is still not clear to me. Can someone explain with example...? Thanks. 回答1: Use a delegate if you want to talk to only one object. For example, a tableView has a delegate - only one object should be responsible for dealing with it. Use notifications if you want to tell everyone that something has happened. For example in low memory situations a notification is sent telling your app that there has been a memory warning. Because lots