开发者

Cocoa: Problem passing a CGPoint with NSNotification and NSDictionary

开发者 https://www.devze.com 2022-12-08 20:10 出处:网络
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\'

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:@"BownceSpriteD开发者_JS百科idSetPosition" 
     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.


Try using +[NSValue valueWithCGPoint].


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


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.


Its been too long time after @Ben Gottlieb have given an answer, his answer is well, but for future I'm keeping an example for reference.

// In example, I want to send CGPoint with notification
[[NSNotificationCenter defaultCenter] postNotificationName:@"MyNotification" object:@{@"someKeyToHoldCGPoint":[NSValue valueWithCGPoint:CGPointMake(10, 10)]}];

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

    //get the dictionary object from notification
    NSDictionary *p = (NSDictionary *)notification.object;

    //get the NSValue object from dictionary p
    NSValue *value = [p valueForKey:@"someKeyToHoldCGPoint"];

    //convert the value to CGPoint 
    CGPoint points = [value CGPointValue];

    //check if we've the correct value
    NSLog(@"%@",NSStringFromCGPoint(points));
}

It should log, (10,10).


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消