Can someone please explain to me what this paragraph means, I couldn’t understand it really well cuz English is not my native laguage.
Notice that when you register as an observer, you can pass a pointer to anything as context. When you are notified of the change, you will receive that same pointer with the notification. The most common use of this is to answer “Is this really the notification that I asked for?” For example, your superclass may use KVO. If you override observeValueForKeyPath:ofObject:change:context:, how do you know which notifications should be forwarded on to the superclass’s implementation?
for this method
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
the context parameter from Developer Documentation definition doesn’t help me either to understand the meaning of context
context
The value that was provided when the receiver was registered to receive key-value observation notifications.
what was the value provided? why do we need to provide value?
The example code given here
[petOwner addObserver:self
forKeyPath:@"fido"
options:0
context:&contextForKVO];
doesn’t make any sense to me I mean if context was suppose to be value, its suppose to related to the fido so i expect the value that was provided is the value of fido.
I’m not sure if my question makes any sense but please explain the context to me with example to assist understanding
ibex10
January 26, 2014, 1:54pm
2
Context is used to pass additional information to the observing object when it is sent the observeValueForKeyPath:ofObject:change:context: message. This information is provided by the observing object itself when it registers for the change notification, thus the observing object knows what is in the context and it can use it appropriately.
Here is a contrived example:
// main.m
// KVO
#import <Foundation/Foundation.h>
// -----------------------------------------------
//
@interface Door : NSObject
@property (strong) NSMutableArray *alerts;
- (void)raiseAlert;
@end
@interface SecuritySystem: NSObject
- (void)observeAlertsFrom:(NSObject *)obj name:(NSString *)name;
@end
NSString *const Door1 = @"FrontDoor";
NSString *const Door2 = @"RearDoor";
NSString *const Door3 = @"SideDoor";
// -----------------------------------------------
//
int main (int argc, const char * argv[])
{
@autoreleasepool
{
Door *rearDoor = [[Door alloc] init];
Door *frontDoor = [[Door alloc] init];
Door *sideDoor = [[Door alloc] init];
SecuritySystem *doorSecurity = [[SecuritySystem alloc] init];
[doorSecurity observeAlertsFrom:rearDoor name:Door1];
[doorSecurity observeAlertsFrom:frontDoor name:Door2];
[doorSecurity observeAlertsFrom:sideDoor name:Door3];
[rearDoor raiseAlert];
[frontDoor raiseAlert];
[sideDoor raiseAlert];
}
return 0;
}
// --------------------------------------------------
//
@implementation Door
@synthesize alerts;
- (id)init
{
self = [super init];
self.alerts = [NSMutableArray array];
return self;
}
- (void)raiseAlert
{
[self willChangeValueForKey:@"alerts"];
[alerts addObject:@"alert"];
[self didChangeValueForKey:@"alerts"];
}
@end
// --------------------------------------------------
//
@implementation SecuritySystem
- (void)observeAlertsFrom:(NSObject *)obj name:(NSString *)name
{
// Use the name as our context
[obj addObserver:self forKeyPath:@"alerts" options:0 context:name];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog (@"%s", __func__);
if (context == Door1) {
NSLog (@"---> Door1 Alert: %@", context);
}
else if (context == Door2) {
NSLog (@"---> Door2 Alert: %@", context);
}
else {
NSLog (@"---> Ignore all other doors...");
}
}
@end
The output:
2014-01-26 23:30:36.043 KVO[11962:403] -[SecuritySystem observeValueForKeyPath:ofObject:change:context:]
2014-01-26 23:30:36.045 KVO[11962:403] ---> Door1 Alert: FrontDoor
2014-01-26 23:30:36.045 KVO[11962:403] -[SecuritySystem observeValueForKeyPath:ofObject:change:context:]
2014-01-26 23:30:36.046 KVO[11962:403] ---> Door2 Alert: RearDoor
2014-01-26 23:30:36.046 KVO[11962:403] -[SecuritySystem observeValueForKeyPath:ofObject:change:context:]
2014-01-26 23:30:36.046 KVO[11962:403] ---> Ignore all other doors...