What does this paragraph means?


Can someone please explain to me what this paragraph means, I couldn’t understand it really well cuz English is not my native laguage.

for this method

the context parameter from Developer Documentation definition doesn’t help me either to understand the meaning of 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


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;


@interface SecuritySystem: NSObject

- (void)observeAlertsFrom:(NSObject *)obj name:(NSString *)name;


NSString *const Door1 = @"FrontDoor";
NSString *const Door2 = @"RearDoor";
NSString *const Door3 = @"SideDoor";

// -----------------------------------------------
int main (int argc, const char * argv[])
        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"];

// --------------------------------------------------
@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...");

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...