key-value-observing

NSOperation KVO isFinished

前提是你 提交于 2019-12-05 14:31:54
Im trying to subclass a NSOperation, and read some sample from, they say: when the task finished, using KVO of NSOperation, to finish the operation, code here: [self willChangeValueForKey:@"isFinished"]; [self willChangeValueForKey:@"isExecuting"] finished = YES; executing = NO; [self didChangeValueForKey:@"isFinished"]; [self didChangeValueForKey:@"isExecuting"]; then isFinished get called - (BOOL) isFinished{ return(finished); } anyone could explain this to me? why isFinished gets called, will the isFinished finish the operation? as I understanded, do KVO manually need [self

Observing a value of a static var in a class?

北城余情 提交于 2019-12-05 12:47:22
I have a class with a static var where the current online connection status is stored. I want to observe the value of ConnectionManager.online through other classes. I wanted to do this with KVO , but declaring a static variable as dynamic causes an error: class ConnectionManager: NSObject { dynamic static var online = false // adding 'dynamic' declaration causes error: // "A declaration cannot be both 'final' and 'dynamic' } What is a most elegant way of doing this? Update . This my code for the KVO part: override func viewDidLoad() { super.viewDidLoad() ConnectionManager.addObserver( self,

KVO: +keyPathsForValuesAffecting<Key> doesn't work with (subclass of) NSObjectController

↘锁芯ラ 提交于 2019-12-05 09:39:18
I have a KVO-able class (call it Observee ), which affectedValue dynamic property is affected by affectingValue property. The dependency between the properties is defined by implementing +keyPathsForValuesAffectingAffectedValue method. Setting a value to affectingValue notifies that affectedValue has changed as I expected, unless Ovservee is a subclass of NSObjectController . Full example follows: @interface Observee : NSObject // or NSObjectController @property (readonly, strong, nonatomic) id affectedValue; @property (strong, nonatomic) id affectingValue; @property (strong, nonatomic)

Is it possible to set up KVO notifications for static variables in objective C?

吃可爱长大的小学妹 提交于 2019-12-05 09:28:19
I have class A, with instance variables deriving attributes from cached data, this cached data is represented as a singleton and is part of A (it's essentially a NSDictionary). From time to time, this cache is modified. When this happens, I would like to have all A instances pull new information from the cache the next time they access their attribute , or in other words, invalidate their attribute content. Up to now, each instances is notified manually (using a static array to track members). I'm not a fan. There's might be an option with notification centre, but I'd rather try with KVO. Has

Key-Value Coding

别来无恙 提交于 2019-12-05 09:12:29
I had a straight forward approach of turning Key/Value pairs of an XML excerpt into an NSDictionary when I started a simple iPhone application. The problem is, I need to turn those NSDictionary's instances that once populated my UITableView's into custom classes because they require behavior and additional complexity. The problem here is that now in order for me to instantiate an object and fill its instance variables with key/value pairs from a web service becomes that much more difficult. I can no longer throw it into a method that iterates through the XML and uses KVC to set its instance

Observing a UISlider's value - iPhone KVO

感情迁移 提交于 2019-12-05 08:26:54
By default, when I observe the value of a UISlider, it only updates once, when the slider is clicked, not continuously, even thought that is the slider's setting. Is there a way to get the continuous value change of the slider? UIKit doesn't actively support KVO. You may be getting lucky in that some notifications may make it through the usual mechanisms, but for the most part you shouldn't assume you can use KVO with any UIKit class. You should instead get your continuous events through the UISlider's associated target's action method. the continuous updating applies to the calling of the

Observing change in UIDatePicker

蹲街弑〆低调 提交于 2019-12-05 08:26:47
问题 I noticed that there is no delegate to observe changes in UIDatePicker. Is there a way to detect when a change is made in the picker without confirming anything, like the moment it spins and lands on a new number I want to be able to detect that. I thought about key value observing, but I don't think there's a property that changes on the spot 回答1: Go to IB and drag from the UIDatePicker to your .h file. Then select Handle this however you want in your .m file; XCode will add the method below

Core Data: Emitting KVO notifications for transient, derived properties

↘锁芯ラ 提交于 2019-12-05 05:04:25
问题 I have Parent entity with a custom class that has a transient and derived (read only) property called DerivedProperty . The value of DerivedProperty is dependent on the value of Parent.IndependentProperty1 and so whenever IndependentProperty1 changes, the value of DerivedProperty will change. However, Parent has a to-many relationship to Child (called children ) and DerivedProperty is also dependent on the value of IndependentProperty2 in all of Parent 's Child objects. So whenever

Bool Property Cannot be marked dynamic in swift

拟墨画扇 提交于 2019-12-05 02:43:46
I'm trying to observe Bool value in swift using KVO and add dynamic modifier like this : dynamic var isRestricted:Bool? and the compiler say Property cannot be marked dynamic because its type canot be represented in Objective-C then what should I do? should I change to NSNumber for this? and What is the best practice for observing value then? im using xcode 7 beta 2 The actual problem is that optional booleans cannot be represented in Objective-C (and therefore not marked dynamic). Using a non-optional dynamic var isRestricted : Bool = false should solve the problem. Generally, the concept of

Do you need to call willChangeValueForKey: and didChangeValueForKey:?

こ雲淡風輕ζ 提交于 2019-12-04 19:27:40
问题 I thought home-cooked @property setters were supposed to look like this: -(void) setFoo:(Foo *)newFoo { // Safeguards // ... [self willChangeValueForKey:@"foo"]; // Switcheroo // ... [self didChangeValueForKey:@"foo"]; } But I see a lot of code in blog posts by people who've been doing Cocoa a lot longer than I have, where it's like this: -(void) setFoo(Foo *)newFoo { // Safeguards // ... // Switcheroo // ... } So my question is, do we need to call the KVO-notification methods? Or is it being