CoreData Swift 和瞬态属性 getter [英] CoreData Swift and transient attribute getters

查看:22
本文介绍了CoreData Swift 和瞬态属性 getter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于在 Swift 中使用 Core Data 时实现计算属性的任何建议?

Any advice on implementing calculated attributes when using Core Data in Swift?

使用生成的 ManagedObject 类,我尝试覆盖 getter,但出现错误:

with the generated ManagedObject class, I tried to override the getter but I get the error:

'NSManaged' 不允许用于计算属性

'NSManaged' not allowed on computed properties

这意味着您不能覆盖瞬态(计算)属性的 getter.

which implies you cannot override the getter for a transient (calculated) attribute.

在下面的代码示例中,dateDue 在我的模型中被定义为一个瞬态属性.

In the code sample below, dateDue is defined as a transient attribute in my model.

请注意,@NSManaged 行是由 Xcode 生成的 - 不是我添加的.

Please note that the @NSManaged lines were generated by Xcode - not added by me.

@NSManaged var timeStamp: NSDate
@NSManaged var dateDue: String { 
    get {

        self.willAccessValueForKey("dateDue")
        var ddtmp  = self.primitiveValueForKey("dateDue") as String?
        self.didAccessValueForKey("dateDue")

        if (ddtmp == nil)
        {

            let calendar = NSCalendar.currentCalendar()

            let components = calendar.components((NSCalendarUnit.YearCalendarUnit | NSCalendarUnit.MonthCalendarUnit ) , fromDate: self.timeStamp)
            ddtmp = "(components.year * 1000 + components.month)"
            self.setPrimitiveValue(ddtmp, forKey: "dateDue")

        }



        return ddtmp!
    }

}

推荐答案

首先,在数据模型中创建一个瞬态属性(section).因为它是暂时的,所以它没有物理存储,因此没有存储在托管对象上下文中.

First, in the data model create a transient attribute (section). Because it is transient, it is not physically stored and thus not stored in the managed object context.

section 属性如下所示:

实体显示在此处:

类 NSManagedObject 子类应该具有计算的 'section' 属性.NSManagedObject 子类演示了如何完成此操作,如下所示:

The class NSManagedObject subclass should have computed 'section' attribute. The NSManagedObject subclass demonstrating how to accomplish this is shown here:

class Number: NSManagedObject {

    @NSManaged var number: NSNumber

    var section: String? {
        return number.intValue >= 60 ? "Pass" : "Fail"
    }
}

然后,您必须在 NSFetchedResultsController 初始化程序中将 sectionForKeyPath 设置为数据模型中的瞬态属性键和缓存名称(如果需要).

Then you must set sectionForKeyPath in the NSFetchedResultsController initializer to be the transient attribute key in the data model and the cache name if desired.

override func viewDidLoad() {
        super.viewDidLoad()

        fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest(), managedObjectContext: managedObjectContext!, sectionNameKeyPath: "section", cacheName: "Root")
        fetchedResultsController?.delegate = self
        fetchedResultsController?.performFetch(nil)

        tableView.reloadData()
}

func fetchRequest() -> NSFetchRequest {

    var fetchRequest = NSFetchRequest(entityName: "Number")
    let sortDescriptor = NSSortDescriptor(key: "number", ascending: false)

    fetchRequest.predicate = nil
    fetchRequest.sortDescriptors = [sortDescriptor]
    fetchRequest.fetchBatchSize = 20

    return fetchRequest
}

结果是一个UITableViewController,成绩按通过或失败动态排序:

The result is a UITableViewController with grades sorted by pass or fail dynamically:

我制作了一个示例项目,可以在 GitHub 上找到.

I made a sample project that can be found on GitHub.

这篇关于CoreData Swift 和瞬态属性 getter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆