Calling an object.method as variables

Hello! Back again!

I am maintaining an object-method pair as host object properties:

    var target : Any? = nil
    var action : Selector

and desire to call the method at some point from my host object:

    if self.target != nil {
        (self.target)?.action
    }

For my attempted call, Xcode is giving me the error:

Type of expression is ambiguous without more context

I have tried various methods of calling the method to no avail. My research in the documentation isn't revealing the right way to do this. Can you please help me figure it out?

Thank you all!

If you want to apply some method dynamically, you may need something like perform(_:).

        _ = (target as AnyObject?)?.perform(action)

Please remember, Swift compiler cannot check if the target can respond to the selector target at compile time. Neither can check the number nor the types of the arguments.

You should better consider using closures if you can touch the class definition.

I agree with @OOPer, you should consider using closures, rather than Selector.
If you post more information, it might be possible to make more specific suggestions?

Calling an object.method as variables
 
 
Q