Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/113.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
Iphone 目标c中普通方法和选择器之间的差异_Iphone_Ios_Objective C_Ipad_Selector - Fatal编程技术网

Iphone 目标c中普通方法和选择器之间的差异

Iphone 目标c中普通方法和选择器之间的差异,iphone,ios,objective-c,ipad,selector,Iphone,Ios,Objective C,Ipad,Selector,在这里我不明白什么是选择器?,但现在我知道它是用来调用方法的,还有人说它是一种回调机制 这两种方法的区别是什么。 实例已创建 Car *porsche = [[Car alloc] init]; 这两种方法中的一种更好 SEL stepTwo = @selector(driveForDistance:); [porsche performSelector:stepOne]; 或 简短答复: Main thingperformSelector允许您动态确定在给定对象上调用选择器的选择器,而无需

在这里我不明白什么是选择器?,但现在我知道它是用来调用方法的,还有人说它是一种回调机制

这两种方法的区别是什么。 实例已创建

Car *porsche = [[Car alloc] init];
这两种方法中的一种更好

SEL stepTwo = @selector(driveForDistance:);
[porsche performSelector:stepOne];

简短答复:


Main thing
performSelector
允许您动态确定在给定对象上调用选择器的选择器,而无需在运行时确定它。但由于两者都是Perfromselector中的一些,所以选择器是方法的名称。message是一个选择器以及随它一起发送的参数。其中方法是选择器和实现的组合

使用
performSelector
可以动态确定在给定对象上调用选择器的选择器。换句话说,选择器不需要在运行前确定

因此,即使这些是等效的:

[theObject aMethod]; 
[theObject performSelector:@selector(theMethod)];
第二个表单允许您执行以下操作:

SEL aSelector = findTheAppropriateSelectorForTheCurrentSituation();
[theObject performSelector: theSelector];
“哪一个更好”-第二个(原文如此)更好。它们有不同的目的

此外,也没有“正常”(或者说“异常”)的方法。有方法。和选择器是唯一的名称识别方法。

如果您不需要动态方法分派,那么就没有理由使用
performSelector:
(更没有理由使用错误的方法-调用只接受一个参数而没有任何参数的方法)。如果知道要对对象调用哪个方法,只需调用它即可


如果您需要反射和动态,那么使用选择器动态解析方法是非常有用和合理的。

选中此复选框这将明确您的概念[here][1][1]:您可以从此链接获得问题的答案
SEL aSelector = findTheAppropriateSelectorForTheCurrentSituation();
[theObject performSelector: theSelector];