Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS observeValueForKeyPath not getting called

I had a problem getting observeValueForKeyPath to be called but solved it using this:

observeValueForKeyPath not being called

However, I'd like to know how to handle this in general for future reference.

The code that works looks like this:

 - (void) input: (NSString*) digit
 {
     NSLog(@"input() - Entering... digit=%@", digit);

     if ([digits length] <  MAX_DIGITS_LENGTH)
     {
         self.digits = [[[ self.digits autorelease] stringByAppendingString:digit] retain];
         NSLog(@"digits is now %@", digits);
     }

 }

Prior to this I was using an NSMutableString instead of NSString and just said used appendString. I didn't do an assignment and I wasn't appending "self" to the digits variable. Are there any good websites/tutorials that explain this more in depth so I know how to do this in general for any type of object?

like image 685
gonzobrains Avatar asked Mar 07 '26 14:03

gonzobrains


1 Answers

KVO works by method swizzling and notifying on value changes. If you access your properties with out self then you are accessing an iVar directly and not using the method. When not using a method there is no way to send KVO notifications to your observer. The other issue you ran into is mutable vs immutable data. When you are appending a string you not changing the object but you are changing the data it is pointing at and that is why you were not getting any notifications. Only the get accessor was being called to get the string then you were calling append data on that.

like image 86
Joe Avatar answered Mar 09 '26 05:03

Joe