Sends a specified message to the receiver and returns the result of the message.
- iOS 2.0+
- macOS 10.0+
- Mac Catalyst 13.0+
- tvOS 9.0+
- watchOS 2.0+
- Objective-C Runtime
A selector identifying the message to send. The message should take no arguments. If
An object that is the result of the message.
perform(_:) method is equivalent to sending the
a message directly to the receiver, which you do in Swift using the dot syntax. For example, the following both do the same thing if
an is an instance of
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:
But use caution when doing this. The
perform(_:) method returns an implicitly unwrapped optional unmanaged pointer to an AnyObject instance
>!). Because it knows nothing about the return value at compile-time, it is up to you to decide how to bring the instance into Swift’s memory management scheme.
Usually, a caller is not responsible for the memory of a returned instance, in which case you use
take, as shown above. However, for any of the creation methods, such as
copy(), the caller is responsible, and you use
take instead. See Memory Management Policy in Advanced Memory Management Programming Guide for a description of ownership expectations.
For more information about using selectors in Swift and alternatives to the
perform(_:) function, read Selectors in Using Swift with Cocoa and Objective-C (Swift 4.1).