[self method] 与 [self performSelector:@selector(method)]之间的区别

如题,既然直接对一个实例发送方法名就可以调用这个方法,那为什么还要使用performSelector:去调呢?两者u有啥区别

阅读 6.1k
1 个回答
the performSelector: method allows you to send messages that aren’t determined until runtime.

比如:

id instance = instance_from_other_module();
[instance call_undeclared_method];   // 有编译警告
[instance performSelector:@selector(call_undeclared_method)];

用这个可以避免编译警告。

此外,performSelector的其他变体功能,可以用来延时调用和跨线程调用。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题