Storing dictionaries in Core Data

Hi @hollance

I’m still working through your great book. I am trying to work out how to save a Dictionary of Data though. In my example I have a Game model that lists the two teams playing as a set, date, a set of players who played, Ideally I need to store a dictionary of how much each player played in that game.

<Player A : 90, Player B : 45...>

I’m struggling with this, do you have any advice? :grinning:

Thanks

Jonny

Core Data works with Entities, and Entities can have relationships to other Entities. That’s the model you have to work within.

You can’t just stick a dictionary in Core Data (other frameworks such as Realm might be more suitable for that).

If you still want to use Core Data, you should translate your dictionary into Entities somehow.

But I think a better question is: why are you using a dictionary for this and not some other data structure? Using dictionaries is what PHP and Python programmers do. :wink: In Swift you’d create a class or struct or some other suitable data structure that describes your data in a more structured way.

Anyway, if I understand what you’re trying to do correctly, you could make a Game entity and a Player entity (maybe also a Team), and make relationships between the Game and the Players who played in that game.

Hi @hollance

Thanks for the reply, that’s pretty much where I’m going, I have this so far, it’s a very basic example but certainly close to being right.

class Player: NSManagedObject {
// the player and which team they play for
@NSManaged var name: String
@NSManaged var team: Team?  
} 

class Team: NSManagedObject {
// the team and which Games they have and which Players are on their team
@NSManaged var name: String
@NSManaged var players: Set<Player> 
@NSManaged var games: Set<Game> 
}

class Game: NSManagedObject {
// the game and which two teams are playing, the date and the Players who played in the game
@NSManaged var teams: Set<Team> 
@NSManaged var date: NSDate
@NSManaged var played: Set<Player>
} 

My Game entity takes the two Teams in a Set, has the date of the game and I have a Set of Players to identify which players played in this game. I believe that is appropriate from my reading.

You are correct! Python and PHP is where I’m coming from, I’m really stuck on how I could save the amount of playing time for each player in each game though without a dictionary, short of building a join table like those in SQL, which I believe is the wrong approach.

What is the accepted iOS Core Data way to do this? :blush:

Thanks

Jonny

I don’t know the answer to your question – I suggest looking in Apple’s Core Data programming guide – but you could always use SQLite directly if you’re more comfortable working with SQL. (But you shouldn’t mix SQL and Core Data: do one or the other.)

Thanks, I’ll look into it and see what I can find :slight_smile:

In your entity have a values that is of type NSData, then convert your dictionary using NSJSONSerialization.dataWithJSONObject…the save the return data into you entity.
hope this helps you

Thanks @stevemcintosh

Certainly a start for me, I will look into this definitely