Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 253
Rob Avatar asked Jul 14 '14 03:07

Rob


1 Answers

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

Update- Swift4
Use @objc tag for Swift 4 as:

@objc var sectionTitle: String? {
   return time!.getTimeStrWithDayPrecision()
}
like image 164
D4ttatraya Avatar answered Oct 10 '22 20:10

D4ttatraya