NStimer使用要点之必备知识点一

来源:互联网 发布:dns中顶级域名有哪些 编辑:程序博客网 时间:2024/05/22 06:20

performSelector苹果官方文档的介绍

先看官方文档的Quick Help,逐字翻译后,后面会有代码对这些令人boring的文档进行解释:

  • Declaration: - (id)performSelector:(SEL)aSelector;
  • Description: Sends a specified message to the receiver and returns the result of the message.

  • 向接收方发送指定的消息,并返回消息的结果

这个方法相当于直接向接收方发送aSelector消息。

For example, the following messages all do the same thing:
Listing 1

id aClone = [anObject copy];id aClone = [anObject performSelector:@selector(copy)];id aClone = [anObject performSelector:sel_getUid("copy")];

The performSelector: method allows you to send messages that aren’t determined until run-time. This means that you can pass a variable selector as the argument:

performSelector: 方法允许您发送直到”运行时”才确定的消息。这意味着您可以传递一个变量选择器作为参数。

(*用直白的话说就是:performSelector在编译的时候发现build success了,如果@selector()中的方法没有声明,不会报错。但是运行时崩溃,
-[Person eat]: unrecognized selector sent to instance 0x174017b60
* Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[Person eat]:
如果直接调用[anObject copy],在编译时编译器就会发现,没有这个方法,就会报错。使用performSelector只会在运行时发现没有这个方法,就崩溃了。所以我们在使用performSelector时最好先看下, whether the receiver implements or inherits a method that can respond to a specified message.所以要用[p respondsToSelector:@selector(eat)]判断*)

Listing 2

SEL aSelector = findTheAppropriateSelectorForTheCurrentSituation();id returnedObject = [anObject performSelector:aSelector];

But use caution when doing this. Different messages require different memory management strategies for their returned objects, and it might not be obvious which to use.

但这样做时要小心。不同的消息返回的对象需要不同的内存管理策略,实际使用时会有坑。

Usually the caller isn’t responsible for the memory of a returned object, but that’s not true when the selector is one of the creation methods, such as copy. See 《Memory Management Policy in Advanced Memory Management Programming Guide》 for a description of ownership expectations. Depending on the structure of your code, it might not be clear which kind of selector you are using for any given invocation.

通常调用者对返回的对象的内存不负任何责任,但是当选择器是创建方法之一(如副本)时,这不是真的。有关所有者期望的描述,请参阅《高级内存管理编程指南》中的内存管理策略。根据代码的结构,您可能不清楚正在为given invocation使用哪种selector。

由于这种不确定性,如果在使用ARC管理内存的同时提供变量选择器,编译器将生成警告。因为它无法在”编译时”确定返回的对象的所有权,所以ARC假设调用者不需要拥有所有权,但这可能不是真的。编译器警告提醒您存在内存泄漏的可能性。

To avoid the warning, if you know that aSelector has no return value, you might be able to use performSelectorOnMainThread:withObject:waitUntilDone: or one of the related methods available in NSObject.

为避免警告,如果您知道aSelector没有返回值,您可能可以使用performSelectorOnMainThread:withObject:waitUntilDone:或NSObject中可用的相关方法之一。

For a more general solution, use NSInvocation to construct a message that you can invoke with an arbitrary argument list and return value.

对于更一般的解决方案,使用NSInvocation来构造一个可以使用任意参数列表和返回值调用的message。

Alternatively, consider restructuring your code to use blocks as a means of passing chunks of functionality through an API. See 《Blocks Programming Topics》 for details.

或者,考虑重组您的代码以使用blocks作为通过API传递功能块的方法。有关细节,请参阅《Blocks Programming Topics》。

  • Paramaters: aSelector
    A selector identifying the message to send. The message should take no arguments. If aSelector is NULL, an NSInvalidArgumentException is raised.

参数aSelector:标识要发送的消息的选择器。消息不应该没有参数。如果aSelector为NULL,则会引发NSInvalidArgumentException异常

—这里讲到运行时的概念,先写这么多,performSelector的使用,后面再写—

原创粉丝点击