什么是快速等同于_cmd? [英] What is the swift equivalent to _cmd?

查看:118
本文介绍了什么是快速等同于_cmd?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取当前方法名称以在类似于此格式的消息中使用

I want to get current method name to use in a format message similar to this one

[NSExeception raise:NSInternalInconsistencyException format:@"You must override %@ in a subclass", NSStringFromSelector(_cmd)]

另外,我想使用_cmd设置关联对象。我很感激。

Also, I want to use _cmd to set associated object. I appreciate any idea.

推荐答案

没有Swift相当于 _cmd 。没有理由在Swift中使用它。

There is no Swift equivalent of _cmd. There is little reason to use it in Swift.

在Objective-C中考虑 _cmd 。什么时候有用?大多数情况下, _cmd 的值将与代码所在方法的名称相同,因此在编译时已知,并且有不需要运行时值。以下是一些可能的情况,当 _cmd 在Objective-C中有用时:

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一个宏。宏在代码中展开,因此如果宏中出现 _cmd ,则将其插入到使用它的源中,因此方法名称可以在里面使用宏。但是,Swift中不存在这样的宏。加上宏是编译时,所以像 __ FUNCTION __ 这样的编译时机制也会起作用。

  • 你可以定义一个C函数获取 self _cmd ,并使用它(相同的函数)作为多种方法的实现,通过使用它添加它 class_addMethod class_replaceMethod ,以及函数内的 _cmd 帮助区分不同的方法调用。然而, class_addMethod class_replaceMethod 不提供迅捷。

  • 方法混写也是一个与方法实现相混淆的过程。由于在调配中你交换了两种方法的实现, _cmd 将有助于揭示调用中使用的实际方法名称,这可能与代码所在的方法不匹配。源代码,因为实现是交换的。我想在Swift中仍然可以进行方法调整,因为在Swift中仍然可以使用 method_exchangeImplementations 。但在方法混写,你在它与交换,因此如果它被称为该方法适合交换的方法,没有歧义,其方法名被调用。

  • 在您手动获取方法的 IMP (实现函数),并使用其他选择器手动调用它的情况。在这种情况下,函数内部可以在 _cmd 中看到不同的选择器。但是,您不必在Swift中担心这一点,因为获取 IMP 的方法不可用。

  • 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 using class_addMethod and class_replaceMethod, and the _cmd inside the function will help distinguish between the different method calls. However, class_addMethod and class_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, since method_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 the IMP are unavailable.

这篇关于什么是快速等同于_cmd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆