Search code examples
iphoneobjective-cenumsnsnotification

Wrong value from NSNumber to enum


I'm sending a notification with enum inside:

[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTitle object:nil userInfo:@{[NSNumber numberWithInt:option2]:kNotificationName}];

And receiving it:

- (void)myAction:(NSNotification *)notification {
MyState myState = (MyState)[[[notification userInfo] objectForKey:kNotificationName] integerValue];

The problem is that myState has wrong value. When i print notification i get:

Printing description of notification:
NSConcreteNotification 0x123456 {name = notificationTitle; userInfo = {2 = notificationName;}}

But myState == option0.

Why it's happening like this?

EDIT:

typedef enum myStates {
  option0,
  option1,
  option2,
  option3
} MyState;

Solution

  • It looks like you got the value and key switched around.

    The dictionary should be used like this: @{key:value}

    Try this:

    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTitle object:nil userInfo:@{kNotificationName:[NSNumber numberWithInt:option2]}];
    

    Hope it helps :)