Search code examples
iosnsnotificationcenternsnotifications

Creating my own NSNotificationCenter, storing objects?


I'm trying to create my own NSNotificationCenter just to get better at programming. I am running into an EXC_BAD_ACCESS that I can't quite work out.

My method:

- (void)addObserver:(id)observer forKey:(NSString *)theKey withSelector:(SEL)block {
    NSString *selector = NSStringFromSelector(block);
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:observer, @"o",selector, "s", nil];
    [[observers objectForKey:theKey] addObject:dict];
}

I get the error in the initialization of NSDictionary *dict, and I don't understand why. The observer being added is a UIViewController which is calling this method in it's viewDidLoad which runs in applicationDidFinishLaunching, if that matters.

I get error EXC_BAD_ACCESS code=1

Any help would be appreciated,.

Cheers.


Solution

  • NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:observer, @"o",selector, "s", nil];
    

    Looks like you are missing an @ on "s" to me.

    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:observer, @"o",selector, @"s", nil];
    

    For your convenience.