在observeValueForKeyPath中无法识别ios swift CMutableVoidPointer

栏目: Swift · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/24640017/ios-swift-cmutablevoidpointer-not-recognized-in-observevalueforkeypath
我试图在我的 Swift

类中使用NSObject(NSKeyValueObserving),但我遇到了类型问题. Xcode抱怨它不理解以下代码中’context’参数的CMutableVoidPointer类型:

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

我使用CMutableVoidPointer,因为Objective-C定义将’context’参数键入为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)
    }
}

翻译自:https://stackoverflow.com/questions/24640017/ios-swift-cmutablevoidpointer-not-recognized-in-observevalueforkeypath


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

More Eric Meyer on CSS (Voices That Matter)

More Eric Meyer on CSS (Voices That Matter)

Eric A. Meyer / New Riders Press / 2004-04-08 / USD 45.00

Ready to commit to using more CSS on your sites? If you are a hands-on learner who has been toying with CSS and want to experiment with real-world projects that will enable you to see how CSS......一起来看看 《More Eric Meyer on CSS (Voices That Matter)》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具