How do I store a Swift Struct in Core Data on iOS?

前端 未结 1 722
傲寒
傲寒 2020-12-30 07:04

At the moment I\'m storing one of my classes as NSData in a plist and obviously that isn\'t a good way to do it.

So I\'m trying to set up Core Data, but my class has

相关标签:
1条回答
  • 2020-12-30 07:55

    I will proceed like this :

    import Foundation
    import CoreData
    
        struct Stop {
           var name: String;
           var id: String;
        }
    
        class Favourite: NSManagedObject {
    
            @NSManaged var name: String
            @NSManaged var id: String
    
            var stop : Stop {
               get {
                    return Stop(name: self.name, id: self.id)
                }
                set {
                    self.name = newValue.name
                    self.id = newValue.id
                }
             }
        }
    

    Very interesting article, you should read: http://www.jessesquires.com/better-coredata-models-in-swift/

    0 讨论(0)
提交回复
热议问题