OC运行时01-MessageSend:

MessageSend:

1,在方法没有实现的时候不会报错,是因为OC运行时机制,在运行的时候才会动态监测,而c或者C++这类语言必须在定义的时候明确指明其类型,而不是等到运行过程动态监测,这类语言叫做静态语言
[cat play];

[cat playWithKid:@"zhangdanfeng"];

[IMCat play];

[IMCat playWithKid:@"zhangdanfeng"];
2,用稍微底层的方法performSeletor实现:
[cat performSelector:@selector(play)];

[cat performSelector:@selector(playWithKid:) withObject:@"zhangdanfeng"];

Class catClass = [cat class];

[catClass performSelector:@selector(play)];

[[IMCat class] performSelector:@selector(playWithKid:) withObject:@"zhangdanfeng"];
3,底层实际就是转成了messageSend方法,貌似iOS8之后底层方法被禁用了,如果需要打开,需要在Build Settings中禁用enable strict checking of Obj-c messageSend calls选项
objc_msgSend(cat, @selector(play));
objc_msgSend(cat, @selector(playWithKid:),@"zhangdanfeng");

objc_msgSend([IMCat class], @selector(play));
objc_msgSend([IMCat class], @selector(playWithKid:),@"zhangdanfeng");

你可能感兴趣的:(OC运行时01-MessageSend:)