Rob Rob - 4 months ago 30
iOS Question

Core data transient values with Swift

Does anyone know, or have an example of, how to handle core data transient values with Swift? I know to use @NSManaged before the properties, but can't figure out how to code the logic to build the transient values using Swift.

Answer

Check mark the transient field in your data model for particular attribute(e.g. sectionTitle).
Create class for that entity, it will look something like

 class Message: NSManagedObject {

    @NSManaged var body: String?
    @NSManaged var time: NSDate?
    @NSManaged var sectionTitle: String?
}

Edit it and make it like this:

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
    }
}