views:

917

answers:

4

I'm trying to fire a Notification in a method called setPosition in one class, that triggers setViewPointCenter in another class. However, I'm trying to send a CGPoint along with it. But Xcode isn't liking it one bit.

-(void)setPosition:(CGPoint)point
{   

    NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"sp", point, nil];

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

    [super setPosition:point];
}

Triggers this method in another class, but throws the indicated error

-(id) init{

    // Usual stuff, blah blah blah...

    [[NSNotificationCenter defaultCenter]
     addObserver:self 
     selector:@selector(setViewPointCenter:) 
     name:@"BownceSpriteDidSetPosition" 
     object:nil];

}

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

    // ERROR: Invalid Initializer
    CGPoint point = [[notification userInfo] valueForKey:@"sp"];


    // more code here....


}

I've been digging around, and found this solution, but I still get an error.

-(void)setPosition:(CGPoint)point
{   
    // ERROR: Incompatile type for argument 1 of "Value With Point"
    NSValue *pointAsObject = [NSValue valueWithPoint:point];
    NSDictionary *dict = [[NSDictionary alloc] 
                         initWithObjectsAndKeys:@"sp", 
                         pointAsObject, 
                         nil];

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

    [super setPosition:point];
}

It's driving me nuts. And to confuse me even further, changing CGPoint to NSPoint like this

-(void)setPosition:(NSPoint)point
{   

    NSValue *pointAsObject = [NSValue valueWithPoint:point];
    NSDictionary *dict = [[NSDictionary alloc] init];
    [dict initWithObjectsAndKeys:@"sp", pointAsObject, nil];

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

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

Get's rid of the error in setPosition, but I'm still screwed in setViewPointCenter. And as I understand it, CGPoint and NSPoint should equal the same thing, but it doesn't look like they do.

Does anyone have a working example of how to pass a CGPoint in a Dictionary? I can't figure it out.

This is for the iPhone, incase that makes a difference.

+1  A: 

You could encapsulate the x and y values from the CGPoint into NSNumber objects using +numberWithFloat: and then add the two resulting NSNumber objects into the dictionary. You can then reconstruct the CGPoint on the other side using:

CGPoint myPoint;
myPoint.x = [myNumberObject floatValue];
myPoint.y = [myNumberObject2 floatValue];

The reason it didn't work in the first try was that CGPoint isn't an object, it's a C struct.

Jasarien
A: 

I haven't read up on GC and NSPoints, but what data-types can NSDictionary hold? Check the docs, maybe you should cast it to NSData.

JoePasq
In other words send the data not the objects.
JoePasq
+3  A: 

I'd use the NSStringFromCGPoint() function to convert it to a string, and then use the CGPointFromString() function to convert it back.

Dave DeLong
+4  A: 

Try using +[NSValue valueWithCGPoint].

Ben Gottlieb
I wish I'd found it sooner, but I stumbled across this, which explains it in more depth. http://stackoverflow.com/questions/1032726/nsnotification-userinfo-example
gargantaun