Note that there are some explanatory texts on larger screens.

plurals
  1. PONSNotificationCenter selector won't work with its NSNotification
    primarykey
    data
    text
    <p>I'm working on a Cocoa project with some C in it (I know, objc contains C...) and am trying to understand <code>NSNotificationCenter</code>s. Here's the situation:</p> <p>I have a struct declared as <code>typedef struct {/*code here*/} structName;</code></p> <p>In my <code>- (id)init</code> method, I have</p> <pre><code>[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selName:) name:@"notName" object:nil]; </code></pre> <p>I have a callback function:</p> <pre><code>int callback(/*args*/) { structName *f = ... NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; [[NSNotificationCenter defaultCenter] postNotificationName:@"notName" object:[[NSValue valueWithPointer:f] retain]]; [autoreleasepool release]; } </code></pre> <p>And then for my selector:</p> <pre><code>- (void)selName:(NSNotification *)note { NSLog(@"here"); NSLog(@"note is %@", note); } </code></pre> <p>Now, if I comment out that second <code>NSLog</code>, everything seems to work (i.e. "here" is printed). But if I leave it in, nothing about the <code>NSNotification</code> seems to work. But this seems to defeat the purpose of the object, userInfo, etc. of the <code>NSNotification</code>.</p> <p>What am I doing wrong and how can I fix it so I can have access to my <code>structName f</code>?</p> <p>@Nathan Okay, so now I have</p> <pre><code>NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSValue valueWithPointer:f] forKey:@"fkey"];//f, not &amp;f. I had a typo in the OP which I fixed. [[NSNotificationCenter defaultCenter] postNotificationName:@"notName" object:nil userInfo:[dict retain]]; </code></pre> <p>...but the problem remains. Any chance this has to do with the typo I fixed?</p> <p>Edit:</p> <p>The problem continues even with changing the two lines above to</p> <pre><code>[[NSNotificationCenter defaultCenter] postNotificationName:@"notName" object:nil userInfo:[NSDictionary dictionaryWithObject:[NSData dataWithBytes:f length:sizeof(structName)] forKey:@"fkey"]]; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload