黑马程序员技术交流社区

标题: 在SEL的使用上有一点点不解 [打印本页]

作者: 陨石    时间: 2014-11-9 23:49
标题: 在SEL的使用上有一点点不解
main.m
  1. #import <Foundation/Foundation.h>
  2. #import "Person.h"


  3. int main(int argc, const char * argv[]) {
  4.     @autoreleasepool {
  5.         // insert code here...
  6.         NSLog(@"Hello, World!");
  7.         Person *p = [[Person alloc]init];
  8.         
  9.         //间接调用对象p的test方法
  10.         SEL s = @selector(test);
  11.         [p performSelector:s];
  12.         [p performSelector:@selector(test)];
  13.       
  14.     }
  15.     return 0;
  16. }
复制代码

person.h
  1. #import <Foundation/Foundation.h>

  2. @interface Person : NSObject
  3. + (void)test;
  4. - (void)test;

  5. @end
复制代码

Person.m
  1. #import "Person.h"

  2. @implementation Person
  3. + (void)test
  4. {
  5.     NSLog(@"Person test");
  6. }
  7. - (void)test
  8. {
  9.     NSLog(@"Person -test");
  10.     /*
  11.      _cmd
  12.      每个方法内部都有一个_cmd,_cmd代表当前方法
  13.      用法
  14.      */
  15.     NSString *str = NSStringFromSelector(_cmd);
  16.     NSLog(@"调用了方法%@",str);
  17.     //死循环
  18.     //[self performSelector:_cmd];
  19. }
  20. /
  21. @end
复制代码


为什么[p performSelector:@selector(test)];会报错。。。百思不得其解。。。
作者: doudou33420    时间: 2014-11-10 01:02
怎么会呢  你报错截图出来 报错的运行结果 和报错行号
作者: 陨石    时间: 2014-11-10 09:44
就第14行啊。。Multiple methods named 'test' found with mismatched result,parameter type or attributes
作者: 陨石    时间: 2014-11-10 09:46
不用test就好使。。。为什么,哪里冲突了。。
作者: 陨石    时间: 2014-11-10 09:54
把test只留一个类方法,然后就报这个错了。。。。如果留对象方法,还报刚才那个错。。*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Person test]: unrecognized selector sent to instance 0x100104fd0'
作者: 银河雨    时间: 2014-11-10 11:40
陨石 发表于 2014-11-10 09:54
把test只留一个类方法,然后就报这个错了。。。。如果留对象方法,还报刚才那个错。。*** Terminating app  ...

只留类方法,肯定要报这个错,因为14行调用的是对象方法。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2