func perform(Selector!, with: Any!) -> Unmanaged<AnyObject>!
func perform(Selector!, with: Any!, with: Any!) -> Unmanaged<AnyObject>!
Availability
Technology
aSelector
A selector identifying the message to send. The message should take no arguments. If a
is NULL
, an invalid
is raised.
An object that is the result of the message.
Calling the 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 My
:
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:
But use caution when doing this. The perform(_:)
method returns an implicitly unwrapped optional unmanaged pointer to an Any
instance (
Unmanaged
<
Any
>!)
. 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.
Important 重要
Because of the inherent lack of type safety, this API is not recommended for use in Swift unless your code specifically relies on the dynamic method resolution provided by the Objective-C run-time.
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).
func perform(Selector!, with: Any!) -> Unmanaged<AnyObject>!
func perform(Selector!, with: Any!, with: Any!) -> Unmanaged<AnyObject>!