Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在iOS中调用方法有什么区别_Objective C - Fatal编程技术网

Objective c 在iOS中调用方法有什么区别

Objective c 在iOS中调用方法有什么区别,objective-c,Objective C,假设我打电话给你 [self methodname] 和其他 [self performSelector:@selector(methodname) withObject:nil]; 没什么区别 直接从performSelector: performSelector:方法相当于直接向接收方发送aSelector消息。例如,以下三条消息的作用相同: id myClone = [anObject copy]; id myClone = [anObject performSelector:@se

假设我打电话给你

[self methodname] 
和其他

[self performSelector:@selector(methodname) withObject:nil];

没什么区别

直接从
performSelector:

performSelector:
方法相当于直接向接收方发送
aSelector
消息。例如,以下三条消息的作用相同:

id myClone = [anObject copy];
id myClone = [anObject performSelector:@selector(copy)];
id myClone = [anObject performSelector:sel_getUid("copy")];
尽管在具体情况下没有区别,但是,
performSelector:
之所以存在,是因为它允许调用编译时可能不可用的任意选择器,如文档中所述:

但是,
performSelector:
方法允许您发送直到运行时才确定的消息。变量选择器可以作为参数传递:

SEL myMethod = findTheAppropriateSelectorForTheCurrentSituation();
[anObject performSelector:myMethod];
上述注意事项也适用于两种变体
performSelector:withObject:
performSelector:withObject:withObject:

还请注意,对于另一组方法,即

  • performSelector:withObject:afterDelay:
  • performSelector:withObject:afterDelay:in模式:
  • performSelectorOnMainThread:withObject:waitUntilDone:
  • performSelectorOnMainThread:withObject:waituntldone:modes:
  • performSelector:onThread:withObject:waituntldone:
  • performSelector:onThread:withObject:waituntldone:modes:
  • PerformSelect背景:withObject:

此处的进一步信息:

还值得注意的是,基本的
性能选择器:…
消息实际存在的原因是,您可以动态确定在运行时使用哪个选择器,然后要求
-performSelector:
执行它。既然我们有了Obj-C块,传递
SELs
就不那么常见了,但它仍然有它的用途(当然还有向后兼容性)。完全同意运行时信息,显然存在编译时差异。编译器在第一种情况下可以提供比在第二种情况下更好的警告。但正如你所说,最终的运行时结果是一样的。为了让答案更简洁,我怀疑是否要包含这段信息,我决定不包含。不过,我认为这是一个合理的注释,所以我在我的答案中添加了一段。谢谢你的建设性评论。
[self methodname]` is shorter and easier to read, write and comprehend.

[self performSelector:@selector(methodname) withObject:nil]` makes it possible to execute arbitrary selectors. If you save the selector in a variable, then you can execute it later on without knowing the method you invoke.

//Self works like this in oops and self works as setter for your class. It also indicates that u r using getter and setter method.