Jonnny Jonnny - 6 months ago 44
Swift Question

Coredata relationship, save Dictionary

I have a CoreData object called

and it has a relationship called
. I wanted it to hold a dictionary of data. Things I'm not sure about.

1) Does a relationship have to be an
2) How would I save a Dictionary [String: Int] or [Player:Int] to that relationship?

I had tried to change the relationship to
and use

game.playedList = try NSJSONSerialization.dataWithJSONObject(myDictionary, options: [])
} catch {
print("JSON serialization failed: \(error)")

Unacceptable type of value for to-many relationship:

I'm a little unsure on this as it's my first time with CoreData


A CoreData entity's relationship must be to one or more other CoreData entities. You cannot define a relationship between an Entity and a non-CoreData object like a Dictionary.

You can define a new Entity with attributes to store each key in this dictionary (if they are predictable).

Alternately you could store your Dictionary as a Data attribute using a ValueTransformer (see Storing UIColor object in Core Data and similar)