1

我想以 SwiftUIText视图的形式返回一个UnsafeMutableRawPointer,遵守 C 调用约定,以便另一种语言可以 FFI 进入它以供使用。

@_cdecl("hi")
func hi (txt: UnsafeMutablePointer<CChar>) -> () {
   var view = ViewBuilder.buildBlock(Text(String(cString: txt)))
   return UnsafeMutableRawPointer(Unmanaged.passUnretained(view).toOpaque())
}

但我收到以下错误:

Cannot invoke 'passUnretained' with an argument list of type '(Text)'

如何获取 SwiftUIText对象的底层指针?

4

1 回答 1

1

用你自己的类包装它

@_cdecl("hi")
func hi (txt: UnsafeMutablePointer<CChar>) -> UnsafeMutableRawPointer {
   let view = ViewBuilder.buildBlock(Text(String(cString: txt)))
    return UnsafeMutableRawPointer(Unmanaged.passUnretained(Wrapper(text: view)).toOpaque())
}

final class Wrapper {
    let text: Text
    init(text: Text) {
        self.text = text
    }
}
于 2019-11-14T08:05:41.223 回答