有没有办法固定在 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 theobjc
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/