Ryan Sands
Ryan Sands

Reputation: 23

Storing and accessing object array in dictionary - Swift 3 - macOS

So I'm not 100% sure if this is even the right way about this but I have a class that stores a few variables, I then have an array that stores theses objects.

I've been trying to put these objects into a dictionary for easy understanding so that I don't have to have several arrays, the idea being I could use the dictionary key to identify which set of arrays I want.

For example I have

class Player {

init(FirstName: String, LastName:String, Team: String, Pos: String, Line: Int, GaP: Int, Sh: Int, ShotPerc: Float, TOIPerG: Float, ShiPerG: Float, TOIPerShi: Float, FaceOffPer: Float, HitA: Int, HitF: Int, SavePerc: Float){
    lastName = LastName
    firstName = FirstName
    team = Team
    pos = Pos
    line = Line
    GP = GaP
    sh = Sh
    shotPerc = ShotPerc
    TOIPerGame = TOIPerG
    shiftsPerGame = ShiPerG
    TOIPerShift = TOIPerShi
    faceOffPerc = FaceOffPer
    hitA = HitA
    hitF = HitF
    savePerc = SavePerc
}

I then store these in separate arrays filtered by the team (only showing relevant code but I parse a CSV file into array of Player and then use filter to put them into appropriate array)

players.append(Player(FirstName: record["First Name"]!, LastName: record["Last Name"]!, Team: record["Team"]!, Pos: record["Pos"]!, Line: Int(record["Line"]!)!, GaP: Int(record["GP"]!)!, Sh: Int(record["Sh"]!)!, ShotPerc: Float(record["ShotPerc"]!)!, TOIPerG: Float(record["TOIPerGame"]!)!, ShiPerG: Float(record["ShiftsPerG"]!)!, TOIPerShi: Float(record["TOIPerShift"]!)!, FaceOffPer: Float(record["FOffPerc"]!)!, HitA: Int(record["HitA"]!)!, HitF: Int(record["HitF"]!)!, SavePerc: Float(record["SavePerc"]!)!))

    var FLA = [Player]()
    var TBL = [Player]()
    var DET = [Player]()
    var WSH = [Player]()
    var PIT = [Player]()
    var NYR = [Player]()
    var NYI = [Player]()

This seem messy ergo wanting to put them all into a dictionary but I can only get one object into it, presumably because it's wiping the key. My question is how do I get it to store all of the players under the same key?

var dict: [String: Array<Player>] = [:]

for (player, _) in LAK.enumerated(){
            dict = ["LAK":[LAK[player]]]
        }
        print(dict["LAK"]?[0].fullName())

I'm presuming it's more along the lines of

dict["LAK"] = 

but I feel like I'm going in circles!

Thanks!

Upvotes: 2

Views: 1188

Answers (2)

Rahul
Rahul

Reputation: 2100

If you want to store to store all players under same key then first collect all players and then add it to the corresponding key.

let suppose you read player from csv. I am writing pseudo code.

   // For playerData in readPlayerFormCSV()
   // Now you will create a Player Object using data
   // player = Player(playerData) 

   // One you have the player object
   // create the dictionary where you want to store them
   var dict: [String: Array<Player>] = [:]

   // Check which team the player belongs
   // if player belongs to LAK then add it player array with team key.

   dict["LAK"].append(player)

My question is how do I get it to store all of the players under the same key?

If you just want players under a single key, Just add it to the corresponding array. I would not recommend this because the purpose of Dictionary is to store "Key-Value" pairs and adding just a single key doesn't make sense. It better to use an array.

  dict["Your Key"].append(player)

Upvotes: 0

Oskar
Oskar

Reputation: 3702

You cannot store multiple values under the same key. That defeats the whole purpose of Key-Value storage. One key should map to one value.

You can store the array of your players instead, but that kind of defeats the purpose of using a dictionary.

Dictionaries should be used when you have unique keys (identifiers) for objects that you want them accessible under. In your case, you could use the player names for that, provided that they are all unique.

Upvotes: 1

Related Questions