为什么我通过 NSNotifcationCenter 传递的值没有被保留?

发布于 2024-08-08 14:18:34 字数 1416 浏览 6 评论 0原文

我正在尝试通过这样的 NSNotification 发送 CGPoint

-(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)];
}

我已经实现了这样的观察者

-(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;
}

但似乎 CGPoint 是空的,或者可能是 (0,0) 。不管怎样,它都没有达到预期的效果,并且 debugString 显示 point.y 为 0.0。

从我发现的所有例子来看,我看起来做得很好。但显然我不是。谁能将我推向正确的方向并指出我的错误?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

英雄似剑 2024-08-15 14:18:34

你已经在字典中颠倒了你的对象和键。它应该显示

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

为是的,这与您期望的方式完全相反,并且大约每三次创建字典时都会让我感到困扰。

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.

戈亓 2024-08-15 14:18:34

你的问题在这里:

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

它应该是:

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

选择器中的“Objects”位于“Keys”之前,因此你将你的项目列为ObjectA,KeyForObjectA,ObjectB,KeyForObjectB等。

你也泄漏了这个字典,因为你分配/初始化它,但永远不要释放它(我假设您没有使用垃圾收集)。

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).

恍梦境° 2024-08-15 14:18:34

在新的 Objective-C 语法中,更好用:

NSDictionary *dict = @{@"p": [NSValue valueWithCGPoint:point]};

它更容易理解,并且使用 NSValue 而不是 NSString

移除观察者也存在问题。在代码中,您仅使用 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setViewPointCenter:) name:@"BownceSpriteDidSetPosition" object:nil]; 但绝不调用 [[NSNotificationCenter defaultCenter]removeObserver:self];,这会产生严重的崩溃,这将很难调试。我建议您使用库 https://github.com/AllinMobile/AIMObservers 来防止这种崩溃。你可以用这种方式重写你的代码:

__weak __typeof(self) weakSelf = self;
self.observer = [AIMNotificationObserver observeName:@"BownceSpriteDidSetPosition" onChange:^(NSNotification *notification) {
   NSValue *valueOfPoint =  [notification userInfo][@"p"];
   CGPoint point = [valueOfPoint CGPointValue];
   CGPoint centerPoint = ccp(240, 160);
   viewPoint = ccpSub(centerPoint, point);
   //use weakSelf to avoid strong reference cycles
   weakSelf.position = viewPoint;
}];

In new objective-c syntax is better to use:

NSDictionary *dict = @{@"p": [NSValue valueWithCGPoint:point]};

it easier to understend and it use NSValue instead of NSString.

There is also a problem with removing observer. In your code, you only use [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setViewPointCenter:) name:@"BownceSpriteDidSetPosition" object:nil]; but never call [[NSNotificationCenter defaultCenter] removeObserver:self];, whitch can produce nasty crash, that would be hard to debug. I sugest you using library https://github.com/AllinMobile/AIMObservers that prevents this kind of crash. You could rewrite your code in that way:

__weak __typeof(self) weakSelf = self;
self.observer = [AIMNotificationObserver observeName:@"BownceSpriteDidSetPosition" onChange:^(NSNotification *notification) {
   NSValue *valueOfPoint =  [notification userInfo][@"p"];
   CGPoint point = [valueOfPoint CGPointValue];
   CGPoint centerPoint = ccp(240, 160);
   viewPoint = ccpSub(centerPoint, point);
   //use weakSelf to avoid strong reference cycles
   weakSelf.position = viewPoint;
}];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文