我正在尝试为使用Kotlin / Native编写的多平台库的iOS部分中给定键的UserDefaults更改实现观察者。我需要实现的功能的Swift签名是:
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
但是似乎Kotlin / Native端没有
UnsafeMutableRawPointer
的映射。我该如何实现?我想移植到Kotlin的代码快速代码如下:
let globalDataStore = UserDefaults(suiteName: "global")
func setObserver() {
globalDataStore?.addObserver(self, forKeyPath: "StringKey", options: NSKeyValueObservingOptions.new, context: nil)
}
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
print("observeValue: \(change?[NSKeyValueChangeKey.newKey] ?? "NO VLAUE")");
}
最佳答案
正如我在文档中发现的那样,objt_code方法的Objective-C版本将observeValue
类型的参数用作void *
。此类型已作为context
映射到Kotlin / Native(请参阅this文档的指针类型子节)。没有COpaquePointer
表示,因为K / N当前仅提供与Objective-C的互操作性。
关于kotlin - 如何在Kotlin/Native中使用UnsafeMutableRawPointer?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60771835/