key-value-observing

Best practices for context parameter in addObserver (KVO)

南楼画角 提交于 2019-11-28 15:06:16
问题 I was wondering what you should set the Context pointer in KVO when you are observing a property. I'm just starting to use KVO and I haven't gleaned too much from the documentation. I see on this page: http://www.jakeri.net/2009/12/custom-callout-bubble-in-mkmapview-final-solution/ the author does this: [annView addObserver:self forKeyPath:@"selected" options:NSKeyValueObservingOptionNew context:GMAP_ANNOTATION_SELECTED]; And then in the callback, does this: - (void)observeValueForKeyPath:

NSNotification VS KVO

给你一囗甜甜゛ 提交于 2019-11-28 10:18:55
I feel that i don't fully understand difference between KVO and NSNotification... They seem to be so similar... Could you make some example showing when is best to use one method and when the other ? I don't speak about Bind and IB, but i mean add Observer programmatically in my code with NSNotificationCenter or KVO [self.preferenceController addObserver:self forKeyPath:@"color" options:NSKeyValueObservingOptionOld context:@"Color-change" ]; KVO only works on values, NSNotification can be used for value changes but it can be used for anything and can carry a much greater payload. For example,

What is the context parameter used for in Key value observing

给你一囗甜甜゛ 提交于 2019-11-28 05:22:05
What's the use of context parameter in following method which is used to register for key value notifications. The documentations just denotes it as arbitrary set of data. addObserver:self forKeyPath:@"selectedIndex" options:NSKeyValueObservingOptionNew context:nil Can somebody shed some light what's the purpose behind it ... I hope this explanation isn't too abstract to understand. Suppose you create a class MyViewController , which is a subclass of UIViewController . You don't have the source code of UIViewController . Now you decide to make MyViewController use KVO to observe changes to the

KVO - How to check if an object is an observer?

纵饮孤独 提交于 2019-11-28 03:40:14
When observing a value on an object using addObserver:forKeyPath:options:context: , eventually you'll want to call removeObserver:forKeyPath: on that object to clean up later. Before doing that though, is it possible to check if an object actually is observing that property? I've tried to ensure in my code that an object is only having an observer removed when it needs to be, but there are some cases where it's possible that the observer may try to remove itself twice. I'm working to prevent this, but just in case, I've just been trying to figure out if there's a way to check first if my code

How reliable is KVO with UIKit

我是研究僧i 提交于 2019-11-27 23:45:00
Important: Not all classes are KVO-compliant for all properties. You can ensure your own classes are KVO-compliant by following the steps described in “KVO Compliance.” Typically properties in Apple-supplied frameworks are only KVO-compliant if they are documented as such. This statement leaves me confused. Can't we use KVO for UIKit objects at all? I don't remember seeing any property being documented as KVO compliant. Despite saying otherwise, I am able to use KVO with many properties. Does this mean that I can't rely on it? Any insight into this would be appreciated. JustSid UIKit is mostly

Javascript object key value coding. Dynamically setting a nested value

不想你离开。 提交于 2019-11-27 21:04:32
问题 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

Key Value Observing - how to observe all the properties of an object?

大兔子大兔子 提交于 2019-11-27 20:33:19
问题 I am happy with the use of Key Value Observing (KVO), and how to register to receive notifications of property change: [account addObserver:inspector forKeyPath:@"openingBalance" options:NSKeyValueObservingOptionNew context:NULL]; However, if I want to observe changes in all the properties of the account object, how can I achieve this? Do I have to register for notification for each and every property? 回答1: It seems there's no built-in function to subscribe for changes in all properties of

How to detect a property return type in Objective-C

醉酒当歌 提交于 2019-11-27 20:08:59
I have an object in objective-c at runtime, from which I only know the KVC key and I need to detect the return value type (e.g. I need to know if its an NSArray or NSMutableArray) of this property, how can I do that? e.James You're talking about runtime property introspection, which happens to be something that Objective-C is very good at . In the case you describe, I'm assuming you have a class like this: @interface MyClass { NSArray * stuff; } @property (retain) NSArray * stuff; @end Which gets encoded in XML something like this: <class> <name>MyClass</name> <key>stuff</key> </class> From

My isa-swizzling breaks KVO

坚强是说给别人听的谎言 提交于 2019-11-27 18:56:08
问题 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

Adding observer for KVO without pointers using Swift

99封情书 提交于 2019-11-27 18:48:14
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: NSObject?, forKeyPath: String?, options: NSKeyValueObservingOptions, context: CMutableVoidPointer)