Adding observer for KVO without pointers using Swift

后端 未结 5 904
灰色年华
灰色年华 2020-12-05 00:28

In Objective-C, I would normally use something like this:

static NSString *kViewTransformChanged = @\"view transform changed\";
// or
static const void *kVie         


        
5条回答
  •  盖世英雄少女心
    2020-12-05 01:27

    Now that KVOContext is gone in Xcode 6 beta 3, you can do the following. Define a global (i.e. not a class property) like so:

    let myContext = UnsafePointer<()>()
    

    Add an observer:

    observee.addObserver(observer, forKeyPath: …, options: nil, context: myContext)
    

    In the observer:

    override func observeValueForKeyPath(keyPath: String!, ofObject object: AnyObject!, change: [NSObject : AnyObject]!, context: UnsafePointer<()>) {
        if context == myContext {
            …
        } else {
            super.observeValueForKeyPath(keyPath, ofObject: object, change: change, context: context)
        }
    }
    

提交回复
热议问题