我要
func foo(inout stop: Bool) -> Void {
// ...
}
在我的 Objective-C 部分使用。但它永远不会在 Module-Swift.h header 中生成。如果我用@objc 标记它,
Method cannot be marked @objc because the type of the parameter cannot be represented in Objective-C
发生错误。
最佳答案
您不能使用 inout
与 Objective-C 桥接时的参数,但如果使用 UnsafeMutablePointer<T>
也可以做类似的事情( T
在您的情况下将是 Bool
)。它看起来像这样:
@objc func foo(stop: UnsafeMutablePointer<Bool>) -> Void {
if stop != nil {
// Use the .pointee property to get or set the actual value stop points to
stop.pointee = true
}
}
例子
测试类.swift:
public class TestClass: NSObject {
@objc func foo(stop: UnsafeMutablePointer<Bool>) -> Void {
stop.pointee = true
}
}
Objective-C :
TestClass *test = [[TestClass alloc] init];
BOOL stop = false;
[test foo:&stop];
// stop is YES here
关于objective-c - 如何在 Objective-C 中使用带有 inout 参数的 Swift 中的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26287921/