I have two objects I am storing in Core Data with swift. List is one object, and Item is another.
Here's the List class:
import Foundation
import CoreData
class List:NSManagedObject {
/* Remember, any @NSMananged variables added or changed here
needs to be reflected in Model */
@NSManaged var name:String
@NSManaged var items: [Item]
}
And here is the mess I've made of the Item class:
import Foundation
import CoreData
class Item:NSManagedObject, NSCoding {
/* Remember, any @NSMananged variables added or changed here
needs to be reflected in Model */
@NSManaged var title:String
@NSManaged var address:String
@NSManaged var notes:String
@NSManaged var quantity:Int32
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(title, forKey: "title")
aCoder.encodeObject(address, forKey: "add")
aCoder.encodeObject(notes, forKey: "notes")
aCoder.encodeInt32(quantity, forKey: "qty")
}
override init(entity: NSEntityDescription, insertIntoManagedObjectContext context: NSManagedObjectContext?) {
}
required init(coder aDecoder: NSCoder) {
title = aDecoder.decodeObjectForKey("title") as! String
address = aDecoder.decodeObjectForKey("add") as! String
notes = aDecoder.decodeObjectForKey("notes") as! String
quantity = aDecoder.decodeInt32ForKey("qty")
}
}
I have read the apple documentation about NSManagedObject, NSObject and NSCoding and it may as well be in a foriegn language.
If I don't implement any of the funtions, it doesn't conform to NSCoding. If I implement encodeWithCoder() and required init() it conforms to NSCoding but then it wants me to add the other init(entity...) for conformance with NSManagedObject. I have no idea what to do with this funtion.
Thanks in advanced for any help. This is really dooing my head in. If you need any more information, I'll edit this post to suit.
NSCoding
? – Wain