使用Swift核心数据瞬态值

有谁知道,或者有一个例子,如何处理核心数据瞬态值与Swift? 我知道在属性之前使用@NSManaged,但不知道如何编写逻辑来使用Swift构build瞬态值。

选中特定属性的数据模型中的瞬态字段(例如sectionTitle )。
创build该实体的类,它会看起来像

  class Message: NSManagedObject { @NSManaged var body: String? @NSManaged var time: NSDate? @NSManaged var sectionTitle: String? } 

编辑它,并使其如下所示:

 class Message: NSManagedObject { @NSManaged var body: String? @NSManaged var time: NSDate? var sectionTitle: String? { return time!.getTimeStrWithDayPrecision() //'getTimeStrWithDayPrecision' will convert timestamp to day //just for eg //you can do anything here as computational properties } } 

更新 – Swift4
使用Swift 4的@objc标签:

 @objc var sectionTitle: String? { return time!.getTimeStrWithDayPrecision() } 

我们应该使用willAccessValueForKey和didAccessValueForKey来支持KVO