通过 UnsafeMutableRawPointer 导出 SwiftUI 视图
Export SwiftUI View via UnsafeMutableRawPointer
我想 return 一个 SwiftUI Text
View as 一个 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)'
如何获取 SwiftUI Text
对象的底层指针?
自己包起来class
@_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
}
}
我想 return 一个 SwiftUI Text
View as 一个 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)'
如何获取 SwiftUI Text
对象的底层指针?
自己包起来class
@_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
}
}