NSNotificationCenter 选择器无法与其 NSNotification 一起使用

发布于 2024-07-26 01:59:44 字数 1655 浏览 6 评论 0原文

我正在开发一个包含一些 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 NSNotificationCenters. 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 技术交流群。

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

发布评论

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

评论(5

难得心□动 2024-08-02 01:59:45

对我有用。 你做了什么不同的事情?

Works for me. What are you doing differently?

浮世清欢 2024-08-02 01:59:45

您应该使用 +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 the NSValue 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 using NSData'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.

你的呼吸 2024-08-02 01:59:45

NSValue 需要一个指向结构的指针,而不是结构本身:

[NSValue valueWithPointer:&f]

NSValue needs a pointer to your struct, not the struct itself:

[NSValue valueWithPointer:&f]

醉梦枕江山 2024-08-02 01:59:45

你说如果你注释掉第二个 NSLog 就可以了。 第二个 NSLog 似乎不正确:

- (void)selName:(NSNotification *)note
{
    NSLog(@"here");
    NSLog(@"note is %@", note);   
}

%@ 格式是打印 NSString 但“note”不是 NSString< /code>,它是一个 NSNotification 对象。 NSNotification 有一个返回 NSString 的 name 属性。 尝试将第二个 NSLog 更改为:

NSLog(@"note is %@", [note name]);

You said it worked if you comment out the second NSLog. And that second NSLog appears to be incorrect:

- (void)selName:(NSNotification *)note
{
    NSLog(@"here");
    NSLog(@"note is %@", note);   
}

%@ format is to print an NSString but "note" is not a NSString, it's an NSNotification object. NSNotification has a name attribute that returns an NSString. Try changing the second NSLog to this:

NSLog(@"note is %@", [note name]);
×纯※雪 2024-08-02 01:59:45

相反,您可以使用以下内容:

- (void)selName:(NSNotification *)note
{
    NSLog(@"here");
    NSLog(@"note is %@", [note userInfo]);
}

Instead, you can use the following:

- (void)selName:(NSNotification *)note
{
    NSLog(@"here");
    NSLog(@"note is %@", [note userInfo]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文