NS_SWIFT_NAME 用于将 C 函数作为类扩展导入

Leo*_*ica 5 swift objective-c-swift-bridge swift3

我有两个 C 函数:

extern UIColor* LNRandomDarkColor();
extern UIColor* LNRandomLightColor();
Run Code Online (Sandbox Code Playgroud)

作为练习,我尝试将它们作为UIColor.

以下为 WWDC 2016 中 Apple 的“Swift 新增功能”演示示例:

void CGContextFillPath(CGContextRef) NS_SWIFT_NAME(CGContext.fillPath(self:)); 
Run Code Online (Sandbox Code Playgroud)

我尝试类似地注释我的函数:

extern UIColor* LNRandomDarkColor() NS_SWIFT_NAME(UIColor.randomDarkColor());
extern UIColor* LNRandomLightColor() NS_SWIFT_NAME(UIColor.randomLightColor());
Run Code Online (Sandbox Code Playgroud)

但是我收到以下警告:

'swift_name' 属性只能应用于带有原型的函数声明

我在这里做错了什么?


更新:为此问题打开SR-2999 。

Mar*_*n R 3

这是部分结果。根据 \n SE-0044 Import as member \xe2\x80\x93\n 处的示例创建的以下代码\n \xe2\x80\x93 进行编译:

\n\n
struct MyColor { };\n\nUIColor * _Nonnull LNRandomDarkColor(void)\n__attribute__((swift_name("MyColor.randomDarkColor()")));\n
Run Code Online (Sandbox Code Playgroud)\n\n

并将 C 函数作为MyColor\ntype 的静态成员函数导入到 Swift:

\n\n
let col = MyColor.randomDarkColor()\n
Run Code Online (Sandbox Code Playgroud)\n\n

但我无法将该函数作为任何现有类型的成员函数导入,例如UIColor

\n\n
UIColor * _Nonnull LNRandomDarkColor(void)\n__attribute__((swift_name("UIColor.randomDarkColor()")));\n// warning: imported declaration \'LNRandomDarkColor\' could not be mapped to \'UIColor.randomDarkColor()\'\n
Run Code Online (Sandbox Code Playgroud)\n\n

(并且UIColor.randomDarkColor()不编译)。我不知道这是一个故意的限制还是一个错误。

\n\n

使用NS_SWIFT_NAME宏而不是swift_name\nattribute 也不起作用:

\n\n
UIColor * _Nonnull LNRandomDarkColor(void)\nNS_SWIFT_NAME("MyColor.randomDarkColor()");\n// warning: parameter of \'swift_name\' attribute must be a Swift function name string\n
Run Code Online (Sandbox Code Playgroud)\n