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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With