key-value-observing

Observing change in UIDatePicker

一世执手 提交于 2019-12-03 22:03:51
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 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 for you. You need to add to your UIDatePicker the UIControlEventValueChanged event to handle date changes:

Core Data: Emitting KVO notifications for transient, derived properties

自古美人都是妖i 提交于 2019-12-03 20:34:38
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 IndependentProperty1 of Parent or IndependentProperty2 of any of the Child objects changes, I would like to

How to observe individual array element changes (update) with Swift and KVO?

谁说胖子不能爱 提交于 2019-12-03 19:15:24
问题 Do I need to subscribe/unsubscribe to individual elements of an array? I want to update each table view cell individually to reflect changes in the backing array. By changes I mean not append/remove operations but update of the properties of the objects of the array. I hope I was able to explain what I want to achieve. Thanks 回答1: To use KVO, declare the model object with dynamic properties: class Foo: NSObject { @objc dynamic var bar: String // in Swift 3, `@objc` is not necessary; in Swift

Array of NSManagedObject attributes

亡梦爱人 提交于 2019-12-03 17:50:16
问题 I'd like to get an array of the attributes for my NSManagedObject so I can use KVO to export them. I can create an array manually and then iterate through it, however, I'd like to get this list automatically, then iterate. 回答1: An NSManagedObject has an entity associated with it. Use NSEntityDescription's -attributesByName and -relationshipsByName . You'll get a dictionary back from each of those methods. Just ask the dicts for their -allKeys . 回答2: Thanks Joshua. Here's code that I used in

KVO on an NSCountedSet?

喜夏-厌秋 提交于 2019-12-03 17:24:04
I'd like to monitor an NSCountedSet to see if its contents changes. Setting up KVO seems to compile but it's not being triggered. First question: can you observe a set? If so then is there something wrong with this message? [subViewA addObserver:subViewB forKeyPath:@"countedSet" options:0 context:NULL]; I'm really just trying to monitor the count of (number of objects in) the set if that helps. Edit - here's the observer (subViewB): - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqual:@"countedSet"])

KVO on Swift's computed properties

自古美人都是妖i 提交于 2019-12-03 15:55:22
just wondering if this is possible in Swift 2.2, KVO on a computed property!? ie: var width = 0 var height = 0 private var area : Double { get { return with * height } } self.addOberser(self, forKeyPath: "area", ...... Would a client code modifying the with or height trigger observeValueForKeyPath? Just checking before engaging on a mayor class refactor. KVO's syntax being as annoying as it's is not worth even a playground if someone has an answer at hand (I am assuming answer is NO) regards! ~d That code won't work for two reasons: You must add the dynamic attribute to the area property, as

KVO differentiating between willChangeValueForKey and didChangeValueForKey - are both necessary?

吃可爱长大的小学妹 提交于 2019-12-03 14:47:46
In line with Apple's own recommendations, when setting KVC/KVO compliant accessors manually, one should include BOTH KVO methods willChange and didChange . This is what I have done in all my manual accessor methods. However, observeValueForKeyPath:ofObject:change:context gets called for each half of the KVC methods (will and did) with exactly the same dictionary contents. When registering an observer using the option: NSKeyValueObservingOptionPrior the observer still gets called twice - once for each half - and, again, with identically the same dictionary contents, save only the difference

How to observe array property changes in RxSwift

▼魔方 西西 提交于 2019-12-03 13:39:08
Here is my class: class ViewController: UIViewController { var myArray : NSArray! } I want to fire an event every time myArray points to a new array, like this: self.myArray = ["a"] self.myArray = ["b"] I've tried rx_observe but failed, here is my code: self.rx_observe(NSArray.self, "myArray").subscribeNext { (array) -> Void in print(array) } It only fires the first time, what's the problem? Most of the time, if you have control of the backing variable, you would prefer Variable to using rx_observe . class ViewController: UIViewController { var myArray : Variable<NSArray>! } The first time you

What's wrong with this observeValueForKeyPath:ofObject:change:context: implementation?

前提是你 提交于 2019-12-03 12:30:02
问题 In my UIScrollView subclass, I'm observing frame changes: [self addObserver:self forKeyPath:@"frame" options:0 context:NULL]; My observeValueForKeyPath:ofObject:change:context: implementation is as follows: - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (object == self && [keyPath isEqualToString:@"frame"]) { [self adjustSizeAndScale]; } if ([UIScrollView instancesRespondToSelector:@selector

NSManagedObject and KVO vs Documentation

て烟熏妆下的殇ゞ 提交于 2019-12-03 11:07:00
问题 I have a custom NSManagedObject subclass, say, Person . I also have a UIView registered with -addObserver:forKeyPath:options:context: to observe various properties of a Person , some of which are persistent like "name" and others are just dumb KVO-compliant accessors unrelated to Core Data, like "drinking". @interface Person : NSManagedObject { BOOL drinking; } @property (nonatomic, retain) NSString* name; @property (nonatomic, readonly) BOOL drinking; @end @implementation Person @dynamic