以下两个方法调用之间的区别

时间:2012-04-23 10:25:59

标签: iphone objective-c selector

[self performSelectorOnMainThread:@selector(customFoo:) withObject:obj waitUntilDone:YES];

[self customFoo:obj];

据我所知,如果在主线程上调用第二个,那么两个之间没有任何区别......是不是?

其中两个有什么基本差异?

2 个答案:

答案 0 :(得分:2)

运行时行为是相同的。但是在编译代码时存在差异:第二个只会在方法customFoo:被定义时编译。

答案 1 :(得分:1)

performSelector:将指定的消息发送给接收方并返回消息的结果。

PerformSelector用于调用您想要执行的方法,这意味着您可以选择执行特定任务的不同选项(方法)示例...

– performSelector:withObject:afterDelay: // will execute method after specific delay.. 
– performSelector:withObject:afterDelay:inModes: 
– performSelectorOnMainThread:withObject:waitUntilDone: 
– performSelectorOnMainThread:withObject:waitUntilDone:modes:
– performSelector:onThread:withObject:waitUntilDone: 
– performSelector:onThread:withObject:waitUntilDone:modes:

– performSelectorInBackground:withObject: //在后台执行任务。所以,你的ManinThread(应用程序)不会停止响应......就像多线程一样..

直接方法([self customFoo:obj];)不会为执行任务提供选择..

For more and detailed explanation visit this reference..

希望,这会对你有帮助......