key-value-observing

Swift 4 approach for observeValue(forKeyPath:…)

与世无争的帅哥 提交于 2020-02-26 04:50:55
问题 I've been trying to find an example, but what I've seen doesn't work in my case. What would be the equivalent of the following code: object.addObserver(self, forKeyPath: "keyPath", options: [.new], context: nil) override public func observeValue( forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { } The code above works, but I get a warning from SwiftLink: Prefer the new block based KVO API with keypaths when using Swift 3.2

Crash with removeObserver:forKeyPath: in Foundation

三世轮回 提交于 2020-01-23 04:19:13
问题 I having some problems with the following crash logs retrieved from the "Crashes" section in Xcode. Only few devices are affected by this crash report. I have analyzed the problem but I guess it's a bug on Apple framework. But I cannot find a way to replicate it. Here a similar discussion: Help with crash in removeObserver:forKeyPath:. Any hints? Thread 0 name: Thread 0 Crashed: 0 Foundation 0x23507591 _NSKeyValueReplaceObservationInfoForObject + 69 (NSKeyValueObserving.m:1166) 1 Foundation

KVO crash on UITextField initWithFrame

≯℡__Kan透↙ 提交于 2020-01-14 03:25:07
问题 A customer of my app sent me this strange crash report and I cannot figure out how this could ever crash. I cannot reproduce the problem myself, so am reliant on the crash report. It crashes on the following very simple line of code: UITextField *value = [[UITextField alloc] initWithFrame:frame]; The crash seems to be related to key-value-observing (KVO), but I am not doing anything with that here; it's a simply constructor! The only possibility I could see, is that another thread is writing

How do I properly observe the contentOffset property of my scrollView subclass?

大城市里の小女人 提交于 2020-01-13 09:21:14
问题 In my iOS app I am observing changes to the contentOffset property of my scrollView subclass. My observer handler looks like this: - (void)observeContentOffsetHandler:(id)aContentOffset { NSLog(@"%@", aContentOffset); } I chose the parameter to the method arbitrarily as an id for simplicity. My NSLog'ging looks like this: -[MyScrollView observeContentOffsetHandler:] [Line 111] NSPoint: {296, 375} -[MyScrollView observeContentOffsetHandler:] [Line 111] NSPoint: {296, 389} -[MyScrollView

KVO differentiating between willChangeValueForKey and didChangeValueForKey - are both necessary?

跟風遠走 提交于 2020-01-12 08:01:06
问题 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

Implement own setter or use KVO?

删除回忆录丶 提交于 2020-01-12 04:53:25
问题 In short, when the property value changing, I have to update some logic in my code, for example: - (void)setProp:(NSString *)theProp { if (prop != theProp){ [prop release]; prop = [theProp copy]; [self myLogic]; } } or: - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if ([keyPath isEqualToString:@"prop"]){ [self myLogic]; } } Which is the best way, and WHY? EDIT: I prefect the second way, because I don't know what

A solution to track a batch of HTTP requests in swift 3.0

百般思念 提交于 2020-01-11 13:19:07
问题 I am using swift 3.0 running under iOS 10.0 and I want to craft some code that fires when a batch condition is met. for i in 0 ..< rex { async code, disappears and does it stuff } Imagine the async code is a collection of URL requests, that basically background as soon as I loop thru them. Now how can I fire off more code when "rex" requests have completed? I thought of setting up a timer to watch and check every second, but its surely not a good solution. I thought kicking off another thread

How to implement KVO for readonly derived NSArray property?

我的未来我决定 提交于 2020-01-06 16:59:07
问题 I'd like to implement KVO for an NSArray property that is declared as readonly . The getter for this readonly property returns a copy of the private NSMutableArray that backs the backs the public readonly one: In my .h : @interface MyClass : NSObject @property (readonly, nonatomic) NSArray *myArray; - (void)addObjectToMyArray:(NSObject *)obj; - (void)removeObjectFromMyArray:(NSObject *)obj; @end And in my .m : @interface MyClass() @property (strong, nonatomic) NSMutableArray *myPrivateArray;

Is it possible to observe the count of an NSDictionary using KVO?

不羁岁月 提交于 2020-01-05 07:56:05
问题 I want to be set the badge value of a UITabItem based on the count of an NSDictionary. I'd like to do this without too much code. KVO seems the way to go, but I can't seem to find anything on simply observing the count of the dictionary, which makes me suspect this is not possible. So, my question: is it possible to observe the count of an NSDictionary using KVO? 回答1: Wrap the dictionary in another object (a proxy) that has a count property that you can observe. You could be all fancy (at the

How to Key-Value-Observe the rotation of a CALayer?

感情迁移 提交于 2020-01-04 05:26:14
问题 I can access the value like this: NSNumber* rotationZ = [myLayer valueForKeyPath:@"transform.rotation.z"]; But for some reason, if I try to KV-observe that key path, I get a compiler error. First, this is how I try to do it: [myLayer addObserver:self forKeyPath:@"transform.rotation.z" options:0 context:nil]; The compiler tells me: *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ addObserver: forKeyPath:@"rotation.z" options:0x0 context:0x528890] was sent to an