0
votes

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.

2
why are you trying to make it conform to NSCoding?Wain
@Wain I was under the impression it had to conform to NSCoding so I could store it in the items:[Item] in the List class. Is this wrong?Roclemir
Yes, this is wrong. If you set up your core data model so that list has a many to one relationship to item then will all be taken care of for youPaulw11

2 Answers

3
votes

You don't need to conform to NSCoding. Core Data will store all of the attributes you have defined in the model without NSCoding and you don't need to implement anything to put things in an array.

BUT!!

If you have set your attribute in core data as transformable then that explains where you're heading, you would need to implement NSCoding, but it can't then also be a managed object. This would also be the wrong approach as you should be using a relationship in the model.

1
votes

Easiest way to clean up the mess:

  • Delete both NSManagedObject subclasses.
  • Set a relationship from List to Item (one to many) and a reverse relationship from Item to List in the Core Data Model.
  • Recreate the NSManagedObject subclasses.

That's it. Now Core Data will manage everything for you. No NSCoding needed.

Edit:

To get an ordered items array for a list object for example sorted by quantity write

let items = (list.items.allObjects as! [Item]).sort{ $0.quantity < $1.quantity }