ios - Swift 的核心数据 transient 值

标签 ios core-data swift

有谁知道或有示例说明如何使用 Swift 处理核心数据 transient 值?我知道在属性之前使用 @NSManaged,但不知道如何编写逻辑代码以使用 Swift 构建 transient 值。

最佳答案

勾选数据模型中特定属性的transient字段(例如sectionTitle)。
为该实体创建类,它看起来像

 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 e.g.
        //you can do anything here as computational properties
    }
}

更新- Swift4
为 Swift 4 使用 @objc 标签作为:

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

关于ios - Swift 的核心数据 transient 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24729190/

相关文章:

ios - 我想使用Xcode中的相同按钮播放多个音频文件

ios - 将对象保存到核心数据中

ios - 在 iOS 中正确访问 ViewController 中的核心数据上下文

swift - 如何在切换 View Controller 后保持 iCloud(CloudKit) 中的数据加载

Swift (IOS) 更新异步函数中的参数

ios - CGImageCreateWithImageProvider:无效的图像尺寸:180 x 180

ios - 如何获取 backBarButtonItem 的宽度?

SwiftUI 如何从所有 View 中查看类

iOS9 UIWebView/Chrome CSS 过渡

ios - 快速判断可选值不起作用