Is it possible to add observers to simple variables such as BOOLs or NSIntegers and see when they change?
Thanks!
Is it possible to add observers to simple variables such as BOOLs or NSIntegers and see when they change?
Thanks!
Yes; the only requirement is that the object in which those variables occur are key-value compliant for those properties.
I believe what you meant was: How to get INT or BOOL value from the 'change' dictionary if the property has changed.
You can simply do it this way:
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary *)change
context:(void *)context
{
if ([keyPath isEqualToString:@"mySetting"])
{
NSNumber *mySettingNum = [change objectForKey:NSKeyValueChangeNewKey];
BOOL newSetting = [mySettingNum boolValue];
NSLog(@"mySetting is %s", (newSetting ? "true" : "false"));
return;
}
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
You observe keys to be notified when their value changes. The data type can be anything. For anything defined as an Objective-C property (with @property in the .h file) this is ready to go so if you want to observe a BOOL property you add to a view controller you do it as follows:
in myViewController.h:
in myViewController.m
in otherViewController.m: