main.m
- #import <Foundation/Foundation.h>
- #import "Person.h"
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- // insert code here...
- NSLog(@"Hello, World!");
- Person *p = [[Person alloc]init];
-
- //间接调用对象p的test方法
- SEL s = @selector(test);
- [p performSelector:s];
- [p performSelector:@selector(test)];
-
- }
- return 0;
- }
复制代码
person.h
- #import <Foundation/Foundation.h>
- @interface Person : NSObject
- + (void)test;
- - (void)test;
- @end
复制代码
Person.m
- #import "Person.h"
- @implementation Person
- + (void)test
- {
- NSLog(@"Person test");
- }
- - (void)test
- {
- NSLog(@"Person -test");
- /*
- _cmd
- 每个方法内部都有一个_cmd,_cmd代表当前方法
- 用法
- */
- NSString *str = NSStringFromSelector(_cmd);
- NSLog(@"调用了方法%@",str);
- //死循环
- //[self performSelector:_cmd];
- }
- /
- @end
复制代码
为什么[p performSelector:@selector(test)];会报错。。。百思不得其解。。。 |