为什么我通过 NSNotifcationCenter 传递的值没有被保留?
我正在尝试通过这样的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你已经在字典中颠倒了你的对象和键。它应该显示
为是的,这与您期望的方式完全相反,并且大约每三次创建字典时都会让我感到困扰。
You've got your objects and keys reversed in the dictionary. It should read
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.
你的问题在这里:
它应该是:
选择器中的“Objects”位于“Keys”之前,因此你将你的项目列为ObjectA,KeyForObjectA,ObjectB,KeyForObjectB等。
你也泄漏了这个字典,因为你分配/初始化它,但永远不要释放它(我假设您没有使用垃圾收集)。
Your problem is here:
It should be:
"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).
在新的 Objective-C 语法中,更好用:
它更容易理解,并且使用
NSValue
而不是NSString
。移除观察者也存在问题。在代码中,您仅使用
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setViewPointCenter:) name:@"BownceSpriteDidSetPosition" object:nil];
但绝不调用[[NSNotificationCenter defaultCenter]removeObserver:self];
,这会产生严重的崩溃,这将很难调试。我建议您使用库 https://github.com/AllinMobile/AIMObservers 来防止这种崩溃。你可以用这种方式重写你的代码:In new objective-c syntax is better to use:
it easier to understend and it use
NSValue
instead ofNSString
.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: