#import <Foundation/Foundation.h>
#define kSomeKey @"key"
#define kNotificationName @"MyMadeUpNameNotification"
@interface Test : NSObject
@end
@implementation Test
-(void) handleNotification:(NSNotification*)notification {
NSString *object = [notification.userInfo objectForKey:kSomeKey];
NSLog(@"%@",object);
}
-(void) run {
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector(handleNotification:)
name: kNotificationName
object: nil];
NSString *anyObject = @"hello";
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:anyObject forKey:kSomeKey];
NSNotification *notification = [NSNotification notificationWithName:kNotificationName object:nil userInfo:userInfo];
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
@end
int main(int argc, char *argv[]) {
@autoreleasepool {
[[Test new] run];
}
}
我認爲用戶信息必須是字典那裏。這不能是一個字符串,你不會插入它。 –
對不起吉姆,你是對的。我這次在CodeRunner中編輯和測試過。 – Jano
所以我明白了。是的,您通過用戶信息提供數據。就像我懷疑的一樣。謝謝Jano。 –