Swift:哪个 class 拥有 NSObject.addObserver(...选项:.New)中使用的 .New 属性?
Swift: which class owns the .New property used in NSObject.addObserver( ... options: .New )?
Swift 中 KVO 的当前最佳实践在 this SO post 中有详细记录,看起来像这样:
someObject.addObserver( self, forKeyPath: "someProperty", options: .New,
context: &self.someContext )
我想通过将其封装在 ObserversManager(比如说)class 中来简化内存管理问题,所以我可以这样做:
func observe ( observed: NSObject, observer: NSObject, keyPath: String,
context: Int )
{
observed.addObserver( observer, forKeyPath: keyPath, options: .New,
context: context ) { ... }
}
func removeObserversByObserver ( observer: NSObject ) { ... }
func removeAllObservers () { ... }
问题是上面的func observer抛出了下面的编译时错误,我目前无力修复,因为官方文档没有详细说明问题的答案。
Could not find member 'New'
错误消息具有误导性。问题是 context:
参数是一个 pointer,而不是 Int:
func observe ( observed: NSObject, observer: NSObject, keyPath: String,
context: UnsafeMutablePointer<Void> )
{
observed.addObserver(observer, forKeyPath: keyPath, options: .New,
context: context)
}
Swift 中 KVO 的当前最佳实践在 this SO post 中有详细记录,看起来像这样:
someObject.addObserver( self, forKeyPath: "someProperty", options: .New,
context: &self.someContext )
我想通过将其封装在 ObserversManager(比如说)class 中来简化内存管理问题,所以我可以这样做:
func observe ( observed: NSObject, observer: NSObject, keyPath: String,
context: Int )
{
observed.addObserver( observer, forKeyPath: keyPath, options: .New,
context: context ) { ... }
}
func removeObserversByObserver ( observer: NSObject ) { ... }
func removeAllObservers () { ... }
问题是上面的func observer抛出了下面的编译时错误,我目前无力修复,因为官方文档没有详细说明问题的答案。
Could not find member 'New'
错误消息具有误导性。问题是 context:
参数是一个 pointer,而不是 Int:
func observe ( observed: NSObject, observer: NSObject, keyPath: String,
context: UnsafeMutablePointer<Void> )
{
observed.addObserver(observer, forKeyPath: keyPath, options: .New,
context: context)
}