views:

92

answers:

2

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?

+4  A: 

Your problem is here:

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

It should be:

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

"Objects" comes before "Keys" in the selector, so you list your items as ObjectA, KeyForObjectA, ObjectB, KeyForObjectB, etc.

You're also leaking this dictionary, since you alloc/init it, but never release it (I'm assuming you're not using garbage collection).

Dave DeLong
memory, that's another thing I'm struggling with. Since I'm subclassing here, i'm guessing I can just override the dealloc method and release this manually?
gargantaun
since you're using `dict` immediately with `NSNotificationCenter`, you're safe to `[dict release]` immediately after posting the notification. That will fix your leak. =)
Dave DeLong
Since it's only used inside that method, just use `[NSDictionary dictionaryWithObjectsAndKeys:pointString, @"p", nil]` and be done with it. You can even pass it straight as the `userInfo:` parameter without storing it to a variable. Simpler, less code, no leaks. That's what autorelease pools are for.
Quinn Taylor
+4  A: 

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.

TechZen
I fear this may not be the last time it bites me either. Thanks.
gargantaun