key-value-observing

My isa-swizzling breaks KVO

这一生的挚爱 提交于 2019-11-29 04:47:00
I'm trying to implement isa swizzling because I need some actions to happen in dealloc method of certain object. I'm overriding - (Class)class; method to return original class (as KVO does). Everything works fine, until I try to add observer to swizzled object. It just crashes. 0x00000000 in 0x00000000 () 0x0091d22a in _NSKeyValueRetainedObservationInfoForObject () 0x0092ec88 in -[NSObject(NSKeyValueObserverRegistration) _addObserver:forProperty:options:context:] () 0x0092d6fd in -[NSObject(NSKeyValueObserverRegistration) addObserver:forKeyPath:options:context:] () Here is implementation of

Receiving 2 KVO notifications for a single KVC change

这一生的挚爱 提交于 2019-11-29 03:13:24
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 is happening and correct it! Does anyone have any ideas why this might be happening? I'm certain I have

NSMutableDictionary KVO

坚强是说给别人听的谎言 提交于 2019-11-29 02:29:33
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)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context

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

半世苍凉 提交于 2019-11-29 02:14:31
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 were added or removed. Here is some example code. This is the class whose array will be observed.

Javascript object key value coding. Dynamically setting a nested value

廉价感情. 提交于 2019-11-28 22:10:51
I'm working on a little library that lets me do some basic key value coding with objects. Say I have the following object: var data = { key1: "value1", key2: { nested1: 1, nested2: "wowza!" } }; And I have the following JavaScript function: var setData = function(path, value) { eval("data." + path + "= value;"); }; And in use: setData("key1", "updated value1"); // data.key1 == "updated value1" setData("key2.nested1", 99); // data.key2.nested1 == 99 This works, however I would like to accomplish the above without using eval . Is this possible, or is eval the best way to go? EDIT: NOTE it can be

How to get notified of changes to models via an NSArrayController?

僤鯓⒐⒋嵵緔 提交于 2019-11-28 21:40:13
问题 I have an NSView subclass which is bound to the arrangedObjects of an NSArrayController . When the array has an item inserted or removed the view is notified. How do I get it to be notified if a model stored in the array has an attribute changed? Do I need to add my view as an observer to every (relevant) attribute of every item added to the array? When an item is added to or removed from the array I am notified via observeValueForKeyPath:ofObject:change:context: in my NSView subclass. I am

Observing Changes to a mutable array using KVO vs. NSNotificationCenter

青春壹個敷衍的年華 提交于 2019-11-28 18:48:43
In my model I have an array of objects called events. I would like my controller to be notified whenever a new object is added to events. I thought that a good way to do this would be use the KVO pattern to get notified when the events changes (from a new object being added) // AppDelegate // events is a NSMutableArray @property/@synthesize etc... [appDelagate addObserver:self forKeyPath:@"events" options:NSKeyValueObservingOptionNew context:NULL]; But the observeValueForKeyPath method wasn't being called and I discovered that Arrays are not KVO compliant :-( One option is to manually trigger

Why does NSOperation disable automatic key-value observing?

∥☆過路亽.° 提交于 2019-11-28 17:56:34
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 for the operation lifecycle flags ( isExecuting etc.) and trigger the KVO events through the accessors,

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

十年热恋 提交于 2019-11-28 16:52:50
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. oxigen #import "objc/runtime.h" unsigned int outCount, i; objc_property_t *properties = class_copyPropertyList([self class], &outCount); for(i = 0; i < outCount; i++) { objc_property_t property = properties[i]; const char *propName = property_getName

KVO vs NSNotification vs protocol/delegates?

自古美人都是妖i 提交于 2019-11-28 15:54:19
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. 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 of objects in your app might want to lower their memory usage it's a notification. I don't think KVO is a