perform(_:)
Sends a specified message to the receiver and returns the result of the message.
Declaration
func perform(_ aSelector: Selector!) -> Unmanaged<AnyObject>!Parameters
- aSelector:
A selector identifying the message to send. The message should take no arguments. If
aSelectorisNULL, an Invalidargumentexception is raised.
Return Value
An object that is the result of the message.
Discussion
Calling the perform(_:) method is equivalent to sending the aSelector message directly to the receiver. For example, the following both do the same thing if anObject is an instance of MyObject:
The perform(_:) 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:
Use caution when doing this. This method returns an implicitly unwrapped optional unmanaged pointer to an AnyObject instance (Unmanaged<AnyObject>!). It’s up to you to decide how to bring the instance into Swift’s memory management scheme. Different messages require different memory management strategies for their returned objects, and it might not be obvious which to use.
Usually, a caller isn’t responsible for the memory of a returned instance, in which case you use takeUnretainedValue(), as shown above. However, for any of the creation methods, such as copy(), the caller is responsible, and you use takeRetainedValue() instead. See Memory Management Policy in Advanced Memory Management Programming Guide for a description of ownership expectations.
Due to this uncertainty, the compiler generates a warning if you supply a variable selector while using ARC to manage memory. Because it can’t determine ownership of the returned object at compile-time, ARC makes the assumption that the caller does not need to take ownership, but this may not be true. The compiler warning alerts you to the potential for a memory leak.
To avoid the warning, if you know that aSelector has no return value, you might be able to use performSelector(onMainThread:with:waitUntilDone:) or one of the related methods available in NSObject.
For a more general solution, use NSInvocation to construct a message that you can invoke with an arbitrary argument list and return value.
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.