How to know when NSHashTable changed count?

时间:2016-01-14 19:50:46

标签: ios swift key-value-observing

I've tried with KVO but looks like did anything wrong. My code below

class A : NSObject  {
var s: String?

init(s: String) {
    super.init()
    self.s = s
    print("\(self.s) init")
}

deinit {
    print("\(self.s) deinit")
}
}

class B : NSObject {
weak var a:A? {
    willSet {
        print("\(self.a?.s) willSet a \(newValue?.s)")
    }
    didSet {
        print("\(self.a?.s) didSet a \(oldValue?.s)")
    }
}

dynamic var hashTable: NSHashTable = NSHashTable.weakObjectsHashTable()

init(a: A?) {
    super.init()
    self.a = a
    print("\(self) init")
    hashTable.addObserver(self, forKeyPath: "count", options: .New, context:nil)
    hashTable.addObject(a)
}

override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    print("observe")
}

deinit {
    print("\(self) deinit")
}

}

Thank you in advance

0 个答案:

没有答案