NSNotificationCenter 选择器无法与其 NSNotification 一起使用
我正在开发一个包含一些 C 的 Cocoa 项目(我知道,objc 包含 C...),并试图理解 NSNotificationCenter
。 情况如下:
我有一个结构体声明为 typedef struct {/*code here*/} structName;
在我的 - (id)init
方法中,我有
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selName:) name:@"notName" object:nil];
一个回调函数:
int callback(/*args*/) {
structName *f = ...
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notName" object:[[NSValue valueWithPointer:f] retain]];
[autoreleasepool release];
}
然后对于我的选择器:
- (void)selName:(NSNotification *)note
{
NSLog(@"here");
NSLog(@"note is %@", note);
}
现在,如果我注释掉第二个 NSLog
,一切似乎都有效(即打印“here”)。 但如果我将其保留,则 NSNotification
似乎不起作用。 但这似乎违背了 NSNotification
的对象、userInfo 等的目的。
我做错了什么以及如何修复它以便可以访问我的 structName f
?
@内森 好吧,现在我已经
NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSValue valueWithPointer:f] forKey:@"fkey"];//f, not &f. I had a typo in the OP which I fixed.
[[NSNotificationCenter defaultCenter] postNotificationName:@"notName" object:nil userInfo:[dict retain]];
……但问题仍然存在。 这有可能与我修正的拼写错误有关吗?
编辑:
即使将上面的两行更改为,问题仍然存在
[[NSNotificationCenter defaultCenter] postNotificationName:@"notName" object:nil userInfo:[NSDictionary dictionaryWithObject:[NSData dataWithBytes:f length:sizeof(structName)] forKey:@"fkey"]];
I'm working on a Cocoa project with some C in it (I know, objc contains C...) and am trying to understand NSNotificationCenter
s. Here's the situation:
I have a struct declared as typedef struct {/*code here*/} structName;
In my - (id)init
method, I have
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selName:) name:@"notName" object:nil];
I have a callback function:
int callback(/*args*/) {
structName *f = ...
NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init];
[[NSNotificationCenter defaultCenter] postNotificationName:@"notName" object:[[NSValue valueWithPointer:f] retain]];
[autoreleasepool release];
}
And then for my selector:
- (void)selName:(NSNotification *)note
{
NSLog(@"here");
NSLog(@"note is %@", note);
}
Now, if I comment out that second NSLog
, everything seems to work (i.e. "here" is printed). But if I leave it in, nothing about the NSNotification
seems to work. But this seems to defeat the purpose of the object, userInfo, etc. of the NSNotification
.
What am I doing wrong and how can I fix it so I can have access to my structName f
?
@Nathan
Okay, so now I have
NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSValue valueWithPointer:f] forKey:@"fkey"];//f, not &f. I had a typo in the OP which I fixed.
[[NSNotificationCenter defaultCenter] postNotificationName:@"notName" object:nil userInfo:[dict retain]];
...but the problem remains. Any chance this has to do with the typo I fixed?
Edit:
The problem continues even with changing the two lines above to
[[NSNotificationCenter defaultCenter] postNotificationName:@"notName" object:nil userInfo:[NSDictionary dictionaryWithObject:[NSData dataWithBytes:f length:sizeof(structName)] forKey:@"fkey"]];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对我有用。 你做了什么不同的事情?
Works for me. What are you doing differently?
您应该使用 +notificationWithName:object:userInfo: 不是 +notificationWithName:object:。
object参数是发送通知的对象。 通常,对于发布通知的对象来说,这将是 self,但由于您从 C 函数调用它,所以它应该为零。
userInfo 参数是一个
NSDictionary
,因此将NSValue
添加到字典中并发送。然后在你的 selName: 方法中获取 -userInfo 来自 NSNotification 并从那里提取您的信息。
注意:当您不应该保留
NSValue
时,您会造成泄漏。编辑:
该结构存在多长时间?
NSValue
不会复制指针的内容,所以它可能被释放? 尝试使用 NSData 的 dataWithBytes:length: 代替。还要确保检查控制台是否有运行时错误(在 Xcode 中:
Run > Console
)。而且你不需要保留 dict。 您可能需要(重新)阅读 Cocoa 内存管理文档。
You should be using +notificationWithName:object:userInfo: not +notificationWithName:object:.
The object parameter is the object sending the notification. Normally this would be self for an object posting the notification but since your calling this from a C function it should be nil.
The userInfo parameter is an
NSDictionary
so add theNSValue
to a dictionary and send that.Then in your selName: method get the -userInfo dict from the NSNotification and pull your info out from there.
Note: You are creating a leak by retaining the
NSValue
when you shouldn't.Edit:
How long does the struct exist?
NSValue
will not copy the contents of the pointer so maybe it's being deallocated? Try usingNSData
's dataWithBytes:length: instead.Also make sure to check the console for runtime errors (in Xcode:
Run > Console
).And you do not need to retain dict. You may want to (re)read the Cocoa memory management docs.
NSValue
需要一个指向结构的指针,而不是结构本身:[NSValue valueWithPointer:&f
]NSValue
needs a pointer to your struct, not the struct itself:[NSValue valueWithPointer:&f
]你说如果你注释掉第二个 NSLog 就可以了。 第二个
NSLog
似乎不正确:%@
格式是打印NSString
但“note”不是NSString< /code>,它是一个 NSNotification 对象。 NSNotification 有一个返回
NSString
的 name 属性。 尝试将第二个NSLog
更改为:You said it worked if you comment out the second
NSLog
. And that secondNSLog
appears to be incorrect:%@
format is to print anNSString
but "note" is not aNSString
, it's an NSNotification object. NSNotification has a name attribute that returns anNSString
. Try changing the secondNSLog
to this:相反,您可以使用以下内容:
Instead, you can use the following: