How do I create an array of a class?
I am managing my data in a singleton database.
The structure is the following:
Main view - Has a table view to select top quarterbacks, top running backs, etc. When the row is clicked, it segues to another view controller displaying the top quarterbacks, running backs, etc.
Top quarterbacks view, runningbacks view, etc.: When the row is swiped, two buttons display - Add, taken -
- Add will add that player to my team.
- Taken will mark the player as taken and add that player to draft order.
After the draft is complete, I have a button called 'add to league'.
- Add to league button will segue to another view controller.
Add league view: This view has a text field to name the league and then a button to add the league.
- Add button adds the league name and segues to another view controller to view the added leagues.
My leagues view: This is a table view that has the added league names. If there are more than one added league, then there will be more than one row in the table view. What I want is when a row is tapped to segue to another view controller with a table view and display that added leagues players and at the same time to reset the draft.
This is how I get the players on my team:
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 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, isTopPlayer: Bool, isRookie: Bool) {
self.num = num
self.numPosPick = numPosPick
self.numRookie = numRookie
self.name = name
self.team = team
self.position = position
self.isTopPlayer = isTopPlayer
self.isRookie = isRookie
}
}
class Database_2019 {
static let shared = Database_2019()
var myLeagues: [String] = []
var myleaguePlayers: [PlayerData]
var myLeaguePlayersData: [[PlayerData]]
func addPlayersToLeague(league: String) {
myleaguePlayers = rosterList()
if myLeagues == [] {
myLeagues.insert(league, at: 0)
myLeaguePlayersData.insert(myleaguePlayers, at: 0)
} else {
myLeagues.append(league)
myLeaguePlayersData.append(myleaguePlayers)
}
resetDraft()
}
// All players and leagues 2019
fileprivate var allPlayers: [PlayerData]
fileprivate var allPlayersReset: [PlayerData] // To hold the reset properties.
fileprivate func playerDataURL() -> URL {
let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask).last!
return documentDirectoryURL.appendingPathComponent("playerData_2019.json")
}
fileprivate func leaguesDataURL() -> URL {
let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask).last!
return documentDirectoryURL.appendingPathComponent("leaguesData_2019.json")
}
fileprivate func leaguePlayersDataURL() -> URL {
let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask).last!
return documentDirectoryURL.appendingPathComponent("leaguePlayersData_2019.json")
}
// Save players and leagues to disk
fileprivate func playersWriteToDisk() {
do {
let data = try JSONEncoder().encode(allPlayers)
try data.write(to: playerDataURL())
} catch let error as NSError {
NSLog("Error reading file: \(error.localizedDescription)")
}
}
fileprivate func leaguesWriteToDisk() {
do {
let data = try JSONEncoder().encode(myLeagues)
try data.write(to: leaguesDataURL())
} catch let error as NSError {
NSLog("Error reading file: \(error.localizedDescription)")
}
}
fileprivate func leaguesPlayersWriteToDisk() {
do {
let data = try JSONEncoder().encode(myLeaguePlayersData)
try data.write(to: leaguePlayersDataURL())
} catch let error as NSError {
NSLog("Error reading file: \(error.localizedDescription)")
}
}
// Load players and leagues from disk
fileprivate func playersReadFromDisk() -> [PlayerData]? {
let filePlayerURL = playerDataURL()
guard FileManager.default.fileExists(atPath: filePlayerURL.path) else {
return nil
}
do {
let fileContents = try Data(contentsOf: filePlayerURL)
let list = try JSONDecoder().decode([PlayerData].self, from: fileContents)
return list
} catch let error as NSError {
NSLog("Error reading file: \(error.localizedDescription)")
}
return nil
}
fileprivate func leaguesReadFromDisk() -> [String]? {
let fileLeaguesURL = leaguesDataURL()
guard FileManager.default.fileExists(atPath: fileLeaguesURL.path) else {
return nil
}
do {
let fileContents = try Data(contentsOf: fileLeaguesURL)
let list = try JSONDecoder().decode([String].self, from: fileContents)
return list
} catch let error as NSError {
NSLog("Error reading file: \(error.localizedDescription)")
}
return nil
}
fileprivate func leaguePlayersReadFromDisk() -> [[PlayerData]]? {
let fileLeaguePlayersURL = leaguePlayersDataURL()
guard FileManager.default.fileExists(atPath: fileLeaguePlayersURL.path) else {
return nil
}
do {
let fileContents = try Data(contentsOf: fileLeaguePlayersURL)
let list = try JSONDecoder().decode([[PlayerData]].self, from: fileContents)
return list
} catch let error as NSError {
NSLog("Error reading file: \(error.localizedDescription)")
}
return nil
}
// Player lists
func playerList(position: String) -> [PlayerData] {
return allPlayers.filter({ $0.position == position })
}
func playerList(topPlayer: Bool) -> [PlayerData] {
return allPlayers.filter({ $0.isTopPlayer == topPlayer })
}
func playerList(rookie: Bool) -> [PlayerData] {
return allPlayers.filter({ $0.isRookie == rookie })
}
// Roster lists/My team lists
func rosterList() -> [PlayerData] {
let rosteredPlayers = allPlayers.filter { ($0.rosterPosition >= 0)}
return rosteredPlayers.sorted(by: { $0.rosterPosition < $1.rosterPosition })
}
func rosterArray() -> [PlayerData] {
let roster = rosterList()
return roster
}
func addToRosterList(player: PlayerData) {
let maxRosteredIndex = allPlayers.map({ $0.rosterPosition }).max()!
player.rosterPosition = maxRosteredIndex + 1
}
fileprivate func removeFromRosterList(player: PlayerData) {
let currentRosterPosition = player.rosterPosition
player.rosterPosition = -1 // No longer on the rosterList. Gets all players on roster at a higher index.
let higherNumberedRosterPlayers = allPlayers.filter({ $0.rosterPosition > currentRosterPosition}) // Their rosterPositions have now gone down by one.
for otherPlayer in higherNumberedRosterPlayers {
otherPlayer.rosterPosition -= 1 // Decrease position by 1.
}
}
// draft List
func draftList() -> [PlayerData] {
let draftedPlayers = allPlayers.filter({ $0.draftPosition >= 0 })
return draftedPlayers.sorted(by: { $0.draftPosition < $1.draftPosition })
}
func addToDraftList(player: PlayerData) {
let maxDraftedIndex = allPlayers.map({ $0.draftPosition }).max()!
player.draftPosition = maxDraftedIndex + 1
}
fileprivate func removeFromDraftList(player: PlayerData) {
let currentDraftPosition = player.draftPosition
player.draftPosition = -1 // No longer on the rosterList. Gets all players on roster at a higher index.
let higherNumberedRosterPlayers = allPlayers.filter({ $0.draftPosition > currentDraftPosition}) // Their rosterPositions have now gone down by one.
for otherPlayer in higherNumberedRosterPlayers {
otherPlayer.draftPosition-=1 // Decrease position by 1.
}
}
func removeFromLists(player: PlayerData) {
removeFromRosterList(player: player)
removeFromDraftList(player: player)
}
fileprivate func removeFromRosterListToBeAdded_2019(player: PlayerData) {
let currentRosterPosition = player.rosterPosition
player.rosterPosition = -1 // No longer on the rosterList. Gets all players on roster at a higher index.
let higherNumberedRosterPlayers = allPlayers.filter({ $0.rosterPosition > currentRosterPosition}) // Their rosterPositions have now gone down by one.
for otherPlayer in higherNumberedRosterPlayers {
otherPlayer.rosterPosition -= 1 // Decrease position by 1.
}
}
// Leagues list to be added to My Leagues
func leaguesList() -> [String] {
return myLeagues
}
func leaguesArray() -> [String] {
let leagues = leaguesList()
return leagues
}
fileprivate func removeFromLeaguesList(player: String) {
let index = IndexPath.Element()
myLeagues.remove(at: index)
}
func removeLeague(league: String) {
removeFromLeaguesList(player: league)
}
// Reset the draft
func resetDraft() {
allPlayers = allPlayersReset
changeMade()
}
// Executed when a change is made
func changeMade() {
playersWriteToDisk()
leaguesWriteToDisk()
}
In line 9 of class Database_2019, I created an array of the class PlayerData but it isn't working.
This is what class myLeague_2019 looks like:
class MyLeague_2019: UIViewController, UITableViewDelegate, UITableViewDataSource {
var players: [[PlayerData]] = [[]]
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
players = Database_2019.shared.myLeaguePlayersData
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return players[section].count
}
func numberOfSections(in tableView: UITableView) -> Int {
return players.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let players = Database_2019.shared.myLeaguePlayersData[indexPath.section][indexPath.row]
let cell = table.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.textLabel?.text = "\(indexPath.row+1). \(players.name), \(players.team) - \(players.position)"
cell.textLabel?.adjustsFontSizeToFitWidth = true
cell.textLabel?.font = UIFont.systemFont(ofSize: 22)
return cell
}
@IBOutlet weak var table: UITableView!
}