English 中文(简体)
为 CoreData 属性/ 属性提供您自己的设置器
原标题:Providing your own setter for a CoreData property / attribute

我有一个具有多个属性的实体, 其中之一是最后修改日期。 当任何对象属性被设置时, 我想更新最后修改日期 。

如果我没有使用核心数据, 我会为这些属性提供我自己的设置, 并更新上一个修改日期。 然而, 我不确定我是否应该和 CoreData 的属性乱搞 。

这样做的最佳方法是什么?

最佳回答

高压安装器很容易完成, 您必须确保您发出正确通知, 以便其他一切工作( 包括KVO) 。

- (void) setThing:(NSObject *)myThing {
  self.lastUpdateDate = [NSDate date];
  [self willChangeValueForKey:@"thing"];
  [self setPrimitiveThing:myThing];
  [self didChangeValueForKey:@"thing"];
}

说到这里,如果你需要做的只是我显示的代码(主要是设定值和更新上一个更新日期),你最好使用Key-Value Survey,并对通知作出反应。它更容易,更干净。

问题回答

如果您重新与NSManagedObject子类合作, 因为这些操作是在运行时提供的( 即 < code> 动力 , 而不是 < code\\ synthesize ) 。 如果您真的想要的话, 您可以推翻属性变异器( 发音器), 但是它会更乱, 没有理由。 相反, 使用密钥值观测( KVO) 。 它会让您知道值何时被更改 。

苹果 s KVO 文件非常丰富: < a href="https:// developmenter.apple.com/library/mac/document/Cocoa/Conceptitual/KeyValueObsession/KeyValueObsesser/KeyValueObsesser.html#/apple_ref/doc/uid/100001775i" rel="nofolpol" >https://developer.apple.com/library/mac/document/Cocoa/Conceptitual/KeyValueObsesser/KeyValueObservice.html#/apple_ref/doc/uid/10000177i





相关问题
How do you create UIBarButtonItems with a radio interface?

I have a UIToolbar that needs three buttons in a radio style, meaning that of the three, only one button can be pushed at a time. The documentation makes reference to the possibility of setting up ...

iPhone settings bundle

I want to allow the user to enter a valid date using the iPhone’s settings application. I have experimented with many of the PreferenceSpecifiers data node types including date. I have two issues: ...

Circular #import/@class problem in ObjectiveC

I m going to use an example to properly illustrate my confusion. I can t quite wrap my head around this. In Cocoa touch, we have UIViewController and its subclass, UINavigationController. Now, UIVC ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Cocoa-Touch: issue looping MPMoviePlayerController

I have an app which has to load some data at startup, so I want to display a splash-screen animation. I m using the MPMoviePlayerController to play a m4v file. The movie has it s background set to [...

Iphone sequential animation with setAnimationDelay

I m trying to chain animation events. The application I m coding for work has a multiple choice quiz. First you pick your multiple choice answer. The quiz view fades away. Then a label ("correct" or "...

热门标签