简体   繁体   English

在 NSManaged 变量上使用属性观察器

[英]Using property observers on NSManaged vars

I have a var declared in a class like so:我在类中声明了一个 var,如下所示:

@NSManaged var isFavorite: Bool

I would like to declare a property observer, very similar to the one below.我想声明一个属性观察者,与下面的非常相似。

 var organization: String {
        didSet { postNotificationWithName( "newData" ) }
    }

However, Swift tells me that having property observers on NSManaged vars is not allowed.但是,Swift 告诉我不允许在NSManaged变量上使用属性观察器。 Is there any way I can implement such a feature or something similar for my isFavorite variable?有什么方法可以为我的isFavorite变量实现这样的功能或类似的功能吗?

Yes-- delete the @NSManaged .是 - 删除@NSManaged It's not absolutely required, but if you delete it you unfortunately need to implement get and set for the property.这不是绝对必需的,但如果您删除它,您很遗憾需要为该属性实现getset You would need to add something like你需要添加类似的东西

The @objc is only needed if you want to be able to do KVO on the property.仅当您希望能够对属性执行 KVO 时才需要@objc

@objc public var newData: String? {
    set {
        willChangeValue(forKey: "newData")
        setPrimitiveValue(newValue, forKey: "newData")
        didChangeValue(forKey: "newData")
    }
    get {
        willAccessValue(forKey: "newData")
        let text = primitiveValue(forKey: "newData") as? String
        didAccessValue(forKey: "newData")
        return text
    }
}

It's kind of annoying to implement both of these if you don't actually need them but that's the way it is for now.如果您实际上不需要它们,那么实现这两个功能有点烦人,但目前就是这样。

Since you'll have a set , you might not need a didSet , but you can still add a didSet if you want one.由于您将拥有一个set ,您可能不需要didSet ,但如果您需要,您仍然可以添加一个didSet

Whoops!哎呀! Paul Patterson is right.保罗帕特森是对的。 What you're supposed to use is Key Value Observing - which is exactly what it says you're supposed to do in the link I suggested.您应该使用的是 Key Value Observing - 这正是它在我建议的链接中所说的。

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html

See also swift notes: https://developer.apple.com/library/mac/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html (use the 'On This Page' menu at the top right of the page for Key-Value Observing)另请参阅 swift 注释: https : //developer.apple.com/library/mac/documentation/Swift/Conceptual/BuildingCocoaApps/AdoptingCocoaDesignPatterns.html (使用页面右上角的“在此页面上”菜单获取键值观察)

So something like所以像

objectToObserve.addObserver(self, forKeyPath: "organization", options: .New, context: &myContext)

paired with配对

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

覆盖NSManagedObjectfunc didChangeValue(forKey key: String)见( https://developer.apple.com/documentation/coredata/nsmanagedobject/1506976-didchangevalue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM