objective-c - Objective-C 有对应的 CF_SWIFT_NAME() 吗?

标签 objective-c swift

有没有办法固定在 Swift 中为 Objective-C 实现的方法的名称?

一个Swift方法

static func convert(foo: Foo) -> Bar

Objective-C 中变为

+ (Bar *)convertWithFoo:(Foo *)foo;

当一个想要的名字是

+ (Bar *)convertFoo:(Foo *)foo;    // without `With`

从 Objective-C 到 Swift,我会使用 CF_SWIFT_NAME()NS_SWIFT_NAME() 宏。

最佳答案

是的,您可以使用 @objc 属性实现:

@objc(convertFoo:)
static func convert(foo: Foo) -> Bar { ... }

来自 "Attributes"在 Swift 引用中:

The objc attribute optionally accepts a single attribute argument, which consists of an identifier. Use this attribute when you want to expose a different name to Objective-C for the entity the objc attribute applies to. You can use this argument to name classes, enumerations, enumeration cases, protocols, methods, getters, setters, and initializers.

关于objective-c - Objective-C 有对应的 CF_SWIFT_NAME() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40931405/

相关文章:

ios - [错误] : unsupported URL in AWS

ios - 单击时如何检查 ImageView 标签

ios - Google Maps iOS Location 位于 MapView 的中心

string - SWIFT:为什么 "NSURL(string:"返回 Nil,即使它在浏览器中是有效的 url?

iOS - 点击按钮后出现矩形

ios - UITextView 每次出现键盘和编辑 TextView 时响应缓慢

ios - 中心位置时在 UICollectionViewCell 上添加掩码

ios - 有没有办法检查 Objective-c 提供的方法的时间复杂度?

iphone - 由于未捕获的异常 'NSGenericException' 而终止应用程序,原因 : '*** Collection <__NSArrayM: 0x117540> was mutated while being enumerated

ios - 如何在 iOS 的 uitableviewcell 中显示消息和图像