这里看不懂什么是selector?,现在才知道是用来调用方法的,还有人说是回调机制。
这两种方法有什么区别。实例已创建
Car *porsche = [[Car alloc] init];
这两种方式中的方法哪一种更好。
SEL stepTwo = @selector(driveForDistance:);
[porsche performSelector:stepOne];
或者
[porsche startEngine];
这里看不懂什么是selector?,现在才知道是用来调用方法的,还有人说是回调机制。
这两种方法有什么区别。实例已创建
Car *porsche = [[Car alloc] init];
这两种方式中的方法哪一种更好。
SEL stepTwo = @selector(driveForDistance:);
[porsche performSelector:stepOne];
或者
[porsche startEngine];
“哪一个更好” - neithier(原文如此)一个更好。他们有不同的目的。
此外,没有“正常”(或“异常”,就此而言)方法。有方法。选择器是唯一名称识别方法。
如果您不需要动态方法分派,那么就没有理由使用performSelector:
(更不用说以错误的方式使用它了——调用一个接受一个参数而没有任何参数的方法)。如果您知道要在对象上调用哪个方法,只需调用它即可。
如果您需要反射和动态,那么使用选择器来动态解析方法是有用且合理的。
简短的回答:
Main thingperformSelector
允许您动态确定在给定对象上调用哪个选择器,而无需确定它Runtime
。但是因为两者都是一些在 Perfromselector 中,所以 Selector 是方法的名称。message 是一个选择器以及您使用它发送的参数。其中方法是选择器和实现的组合。试试这个,有很多关于 SO 的问题。
允许您动态确定在performSelector
给定对象上调用哪个选择器。换句话说,选择器不需要在运行前确定。
因此,即使这些是等价的:
[theObject aMethod];
[theObject performSelector:@selector(theMethod)];
第二种形式允许您这样做:
SEL aSelector = findTheAppropriateSelectorForTheCurrentSituation();
[theObject performSelector: theSelector];