Wednesday, January 26, 2011

Dostoevsky Loves Objective-C

If you have created a dictionary (NSDictionary or NSMutableDictionary) in Raskolnikov.m, and you need to send that information over to Petrovich.m... I would recommend using NSNotificationCenter.

Petrovich.m / Establish the notification for the function that will use the dictionary:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(functionName:) name:@"Petrovich.functionName" object:nil];

Petrovich.m / Establish the function that will use the dictionary:

- (void) functionName : (NSNotification *)notification {

NHLog(@"victim01 = %@", [notification.userInfo objectForKey:@"victim01"]);

}

Raskolnikov.m / Make a dictionary, then call the function in Petrovich.m that will use the dictionary:

NSMutableDictionary *myDictionary = [[NSMutableDictionary alloc] initWithObjectsAndKeys: @"Lizaveta", @"victim01", @"Alyona", @"victim02", nil];

[[NSNotificationCenter defaultCenter] postNotificationName:@"Petrovich.functionName" object:self userInfo:myDictionary];

[myDictionary release];