Why did my value go through unsold NSNotifcationCenter?

advertisements

I'm trying to send a CGPoint through an NSNotification like this

-(void)setPosition:(CGPoint)point
{
 NSString *pointString = NSStringFromCGPoint(point);

 NSDictionary *dict = [[NSDictionary alloc]
                         initWithObjectsAndKeys:@"p", pointString, nil];

 [[NSNotificationCenter defaultCenter]
     postNotificationName:@"BownceSpriteDidSetPosition"
     object:self
     userInfo:dict];

 [super setPosition:CGPointMake(point.x, point.y)];
}

And I've implemented the observer like this

-(void) init
{
    if((self = [self init])){
       [[NSNotificationCenter defaultCenter]
       addObserver:self selector:@selector(setViewPointCenter:)
       name:@"BownceSpriteDidSetPosition"
       object:nil];

       // I wondered wether 'object' should be something else???

       // more code etc....
    }
    return self
}

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

 NSString * val = [[notification userInfo] objectForKey:@"p"];
 CGPoint point = CGPointFromString(val);

    // trying to debug
    NSString debugString = [NSString stringWithFormat:@"YPOS -----> %f", point.y];
 NSLog(debugString);

 CGPoint centerPoint = ccp(240, 160);
 viewPoint = ccpSub(centerPoint, point);

 self.position = viewPoint;
}

But it seems that CGPoint is empty, or (0,0) maybe. Either way, it's not having the desired effect, and the debugString is showing point.y to be 0.0.

From all the examples I've found, it looks to me like I'm doing it all right. But obviously I'm not. Can anyone nudge me in the right direction and point out my mistake?


You've got your objects and keys reversed in the dictionary. It should read

 NSDictionary *dict = [[NSDictionary alloc]
                         initWithObjectsAndKeys:pointString,@"p", nil];

Yes, it's exactly backwards of the way you would expect it to be and this bites me about every third time I create a dictionary.