unrecognized selector的错误问题

来源:互联网 发布:linux socket编程 pdf 编辑:程序博客网 时间:2024/06/08 07:48

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onReceiveSongList) name:@"onReceiveSongList" object:nil]

一开始我是这样写的,就是@selector(onReceiveSongList)这里后面没有带冒号的,结果,Xcode只会说undeclared selector的警告,然后我没有管这个警告,运行时就会报错:

reason: '-[ViewController onReceiveSongList]: unrecognized selector sent to instance 0x7fc672421b60'

后来查了很久,发现少了个冒号:

[[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(onReceiveSongList:)name:@"onReceiveSongList"object:nil]

改成这样就不会出错了。


发送通知的地方是这样写的:

[[NSNotificationCenterdefaultCenter]postNotificationName:@"onReceiveSongList"object:[NSNumbernumberWithBool:YES]];

一开始我把object写成nil,也会有类似上面的错误

-(void)onReceiveSongList:(NSNotification*)notic{    NSLog(@"onReceiveSongList");    if ([notic.object boolValue]) {        JsonProtocol * json = [JsonProtocol share];        NSMutableArray *list = [json getSongList];//        Music *item = [[Music alloc]init];        [_alreadydatasoure removeAllObjects];        for (int i = 0; i < [list count]; i++) {            [_alreadydatasoure addObject:list[i]];        }        [self.tableview reloadData];    }}

从上面的代码看出,参数里面装了一个bool类型的值,这个就是post消息的时候传进来的。如果post的时候传进来一个nil,那么到了执行这个方法的时候就会报错。

0 0
原创粉丝点击