What is the swift equivalent to _cmd?
NSStringFromSelector(_cmd); // Objective-C
print(#function) // Swift 4, 5
There is no Swift equivalent of _cmd
. There is little reason to use it in Swift.
Consider _cmd
in Objective-C. When is it useful? Most of the time, the value of _cmd
would be the same as the name of the method that the code is in, so it is already known at compile time, and there is no need for a runtime value. Here are some possible cases when _cmd
is useful in Objective-C:
- In a macro. The macro is expanded in the code, so if
_cmd
appears in the macro, then it is inserted into the source where it is used, and so the method name can be used inside the macro. However, such macros do not exist in Swift. Plus macros are compile-time, so a compile-time mechanism like__FUNCTION__
would work similarly. - You can define a C function that takes
self
and_cmd
, and use it (the same function) as the implementation of multiple methods, by adding it usingclass_addMethod
andclass_replaceMethod
, and the_cmd
inside the function will help distinguish between the different method calls. However,class_addMethod
andclass_replaceMethod
are not available in Swift. - Method swizzling is also a process that messes with the implementation of a method. Since in swizzling you swap the implementations of two methods,
_cmd
will help reveal the actual method name used in the call, which may not match the method that the code is in in the source code, since implementations are swapped. I guess method swizzling may still be possible in Swift, sincemethod_exchangeImplementations
is still available in Swift. But in method swizzling, the method you swap in is tailored for the method it is swapping with, so if it is called, there is no ambiguity which method name is being called. - In the case where you manually get the
IMP
(implementing function) of a method, and manually call it with a different selector. In this case, the inside of the function can see the different selector in_cmd
. However, you don't have to worry about this in Swift because the methods that get theIMP
are unavailable.