gpt4 book ai didi

ios swift CMutableVoidPointer 在 observeValueForKeyPath 中无法识别

转载 作者:IT王子 更新时间:2023-10-29 05:44:39 26 4
gpt4 key购买 nike

我试图在我的 Swift 类中使用 NSObject(NSKeyValueObserving),但我遇到了类型问题。 Xcode 提示它不理解以下代码中 'context' 参数的 CMutableVoidPointer 类型:

override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: NSDictionary!, context: CMutableVoidPointer)

我使用 CMutableVoidPointer 是因为 Objective-C 定义将“上下文”参数类型化为 void *。

我在编译时遇到的确切错误是:“使用未声明的类型‘CMutableVoidPointer’”。

我正在使用 Xcode Beta 3。

如有任何帮助,我们将不胜感激。

最佳答案

这是根据 Using Swift with Cocoa and Objective-C 制定的当前最佳实践:

// Add the dynamic modifier to any property you want to observe
class MyObjectToObserve: NSObject {
dynamic var myDate = NSDate()
func updateDate() {
myDate = NSDate()
}
}

// Create a global context variable
private var myContext = 0

// Add an observer for the key-path, override the observeValueForKeyPath:ofObject:change:context: method, and remove the observer in deinit.
class MyObserver: NSObject {
var objectToObserve = MyObjectToObserve()
override init() {
super.init()
objectToObserve.addObserver(self, forKeyPath: "myDate", options: .New, context: &myContext)
}
override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject: AnyObject], context: UnsafeMutablePointer<Void>) {
if context == &myContext {
println("Date changed: \(change[NSKeyValueChangeNewKey])")
} else {
super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
}
}
deinit {
objectToObserve.removeObserver(self, forKeyPath: "myDate", context: &myContext)
}
}

关于ios swift CMutableVoidPointer 在 observeValueForKeyPath 中无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24640017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com