Contents (package MULK.OBJECTIVE-CL)
Function INVOKE-BY-NAME
Send a message to an Objective-C object by the name of the method.
invoke-by-name receiver method-name &rest
args
receiver --- an Objective-C wrapper object.
method-name --- a selector designator.
args --- a list of objects.
Returns: result --- the return value of the method invocation.
invoke-by-name is like invoke except in its syntax. It sends the message whose selector is designated by method-name, which must be either a string, a symbol, a list of message name components as in a call to invoke, or an object of type selector, to receiver.
(invoke-by-name (find-objc-class 'ns-string)
'(:string-with-u-t-f-8-string) "Mulk.")
;=> #<GSCBufferString `Mulk.' {5B36087}>
(invoke-by-name (find-objc-class 'ns-object)
"self")
;=> #<NSObject `NSObject' {16ECF598}>
(invoke-by-name (find-objc-class 'ns-string)
"stringWithCString:encoding:"
"Mulk."
4)
;=> #<GSCBufferString `Mulk.' {5B36087}>
selector objects are funcallable. Therefore, the following calls are all equivalent:
(invoke-by-name instance "stringWithCString:encoding:" "Mulk." 4)
(invoke instance :string-with-c-string "Mulk." :encoding 4)
(funcall (selector "stringWithCString:encoding:") instance "Mulk." 4)
In fact, using invoke-by-name is discouraged in favour of the latter form.
Whereas invoke tries to make writing as well as reading method invocations easy by interspersing method name components with arguments as Objective-C does, invoke-by-name is better suited for method selection at run time as well as code generation. It is also slightly easier to use with apply.