1

有问题的代码如下。[我希望]这个问题在上面的问题中得到了完整的概述。提前致谢。MJB。

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate
{
    var some_string:NSString = "one"

    func application ( application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]? ) -> Bool
    {
        println("init...")
        self.addObserver( self, forKeyPath: "some_string", options: nil, context: nil )
        self.some_string = "two"
        return true
    }

    override func observeValueForKeyPath ( keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void> )
    {
        println("observeValueForKeyPath...")
    }
}
4

1 回答 1

3

根据Apple Documentation,您需要使用dynamic关键字为该属性启用 KVO:

dynamic var some_string : NSString = "one"

此外,该类必须继承 fromNSObject才能使其工作,尽管在您的情况下这是一个给定的 with UIResponder

于 2015-03-17T22:24:17.787 回答