ios 通知NSNotificationCenter 的名字可否同名
2015-11-05 15:52
656 查看
最近公司招人,面试了几个人,总是问这么个问题,
当我创建通知时可否创建两个同名的通知,竟没有一个人回答说可以。
经过实测在同一个项目中可以存在多个同名的通知,最后造成的结果是创建了几个这个通知就发送给了各自的通知。
比如我在两个类里面都创建了名为test的通知,
@implementation gkfdd
- (id)init {
self = [super
init];
if (!self) {
return
nil;
}
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(fjsd)
name:@"test"
object:nil];
return
self;
}
- (void)fjsd {
NSLog(@"haha1");
}
@end
@implementation fds
- (id)init {
self = [super
init];
if (!self) {
return
nil;
}
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(fjsd)
name:@"test"
object:nil];
return
self;
}
- (void)fjsd {
NSLog(@"haha2");
}
@end
#import "gkfdd.h"
#import "fds.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
gkfdd *s = [[gkfdd
alloc] init];
fds *d = [[fds
alloc] init];
[[NSNotificationCenter
defaultCenter] postNotificationName:@"test"
object:self];
//[[NSNotificationCenter defaultCenter] postNotificationName:@"haha" object:self userInfo:nil];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
2015-11-05 15:48:29.512 notification[20402:3347336] haha2
所以我觉的程序员要追求“为什么”是很有必要的
当我创建通知时可否创建两个同名的通知,竟没有一个人回答说可以。
经过实测在同一个项目中可以存在多个同名的通知,最后造成的结果是创建了几个这个通知就发送给了各自的通知。
比如我在两个类里面都创建了名为test的通知,
gkfdd类
#import "gkfdd.h"@implementation gkfdd
- (id)init {
self = [super
init];
if (!self) {
return
nil;
}
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(fjsd)
name:@"test"
object:nil];
return
self;
}
- (void)fjsd {
NSLog(@"haha1");
}
@end
fds类
#import "fds.h"@implementation fds
- (id)init {
self = [super
init];
if (!self) {
return
nil;
}
[[NSNotificationCenter
defaultCenter] addObserver:self
selector:@selector(fjsd)
name:@"test"
object:nil];
return
self;
}
- (void)fjsd {
NSLog(@"haha2");
}
@end
ViewController类
#import "ViewController.h"#import "gkfdd.h"
#import "fds.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super
viewDidLoad];
gkfdd *s = [[gkfdd
alloc] init];
fds *d = [[fds
alloc] init];
[[NSNotificationCenter
defaultCenter] postNotificationName:@"test"
object:self];
//[[NSNotificationCenter defaultCenter] postNotificationName:@"haha" object:self userInfo:nil];
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
最后打印出
2015-11-05 15:48:29.512 notification[20402:3347336] haha12015-11-05 15:48:29.512 notification[20402:3347336] haha2
所以我觉的程序员要追求“为什么”是很有必要的
相关文章推荐
- ios的@property属性和@synthesize属性
- iOS关于破解ipa包
- iOS小技巧-为项目添加全局PrefixHeader.pch预编译文件
- iOS_URI跳转方式多种地图导航的代码实践
- iOS 开发过程中 横竖屏显示问题
- iOS学习笔记3---delegate五步曲,轻松学会使用delegate
- 接口参数如果后台返回"<null>",IOS 前台会崩溃,返回“”OK
- iOS开发中视图相关的小笔记:push、modal、popover、replace、custom
- [IOS] ios 字符串 MD5加密
- ios模拟器展示3D Touch
- iOS之系统越狱
- ios里如何进行异常捕捉
- iOS反射的基本应用
- iOS:原生二维码扫描
- Retinize:iOS切图神器
- iOS中判断消息推送是否打开
- RSA加密(1.0)
- IOS之NSAttributedString和NSMutableAttributedString
- iOS 简单的按钮弹出效果
- iOS 蓝牙开发(三)app作为外设被连接的实现