繁体   English   中英

如何在 Kotlin/Native 中使用 UnsafeMutableRawPointer?

[英]How to use UnsafeMutableRawPointer in Kotlin/Native?

我正在尝试对使用 Kotlin/Native 编写的多平台库的 iOS 部分中给定键的 UserDefaults 更改实现观察者。 我需要实现的函数的 Swift 签名是:

override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)

但似乎在 Kotlin/Native 方面没有UnsafeMutableRawPointer

我怎样才能做到这一点? 我想移植到 Kotlin 的代码 swift 代码如下:

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")");
}

正如我在文档中发现的, observeValue方法的 Objective-C 版本使用void *类型参数作为context 这种类型被映射到 Kotlin/Native 作为COpaquePointer (请参阅文档的指针类型小节)。 没有UnsafeMutableRawPointer表示,因为 K/N 目前仅提供与 Objective-C 的互操作性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM