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"]];