How do I read in a csv file and input the data into a class?
Here is my class object code
class PlayerData: Codable {
var num: Int = 0
var numPosPick: Int = 0
var numRookie: Int = 0
var name: String = ""
var team: String = ""
var position: String = ""
var byeWeek: Int = 0
var rosterPosition: Int = -1
var draftPosition: Int = -1
var isTopPlayer: Bool
var isRookie: Bool
var isRostered: Bool { return rosterPosition >= 0 }
var isDrafted: Bool { return draftPosition >= 0 }
init(num: Int, numPosPick: Int, numRookie: Int, name: String, team: String, position: String, byeWeek: Int, isTopPlayer: Bool, isRookie: Bool) {
self.num = num
self.numPosPick = numPosPick
self.numRookie = numRookie
self.name = name
self.team = team
self.position = position
self.byeWeek = byeWeek
self.isTopPlayer = isTopPlayer
self.isRookie = isRookie
}
}
Here is what I want it to look like but I want it to be inserted from the csv file.
allPlayers = [
PlayerData(num: 1, numPosPick: 1, numRookie: 0, name: "Christian McCaffrey", team: "CAR", position: "RB", byeWeek: 13, isTopPlayer: true, isRookie: false),
PlayerData(num: 2, numPosPick: 2, numRookie: 0, name: "Dalvin Cook", team: "MIN", position: "RB", byeWeek: 7, isTopPlayer: true, isRookie: false),
PlayerData(num: 3, numPosPick: 3, numRookie: 0, name: "Saquon Barkley", team: "NYG", position: "RB", byeWeek: 10, isTopPlayer: true, isRookie: false),
PlayerData(num: 4, numPosPick: 4, numRookie: 0, name: "Derrick Henry", team: "TEN", position: "RB", byeWeek: 13, isTopPlayer: true, isRookie: false),
PlayerData(num: 5, numPosPick: 1, numRookie: 0, name: "Tyreek Hill", team: "KC", position: "WR", byeWeek: 12, isTopPlayer: true, isRookie: false)
]
Here is the csv file