Hello,
I made these changes and now I have a lot of errors. What am I doing wrong?
import Foundation
import UIKit
class PlayerData: Codable {
var num: Int = 0
var name: String = ""
var team: String = ""
var position: String = ""
var strikeThrough: Bool = false
var color: Bool = false
var accessory: Bool = false
init(num: Int, name: String, team: String, position: String) {
self.num = num
self.name = name
self.team = team
self.position = position
}
}
var objectsArray = [PlayerData]()
class Database {
static let shared = Database()
fileprivate var allPlayers: [PlayerData]
fileprivate func playerDataURL() -> URL {
let documentDirectoryURL = FileManager.default.urls(for: .documentDirectory, in:.userDomainMask).last!
return documentDirectoryURL.appendingPathComponent("playerData.json")
}
fileprivate func readFromDisk() -> [PlayerData]? {
let fileURL = playerDataURL()
guard FileManager.default.fileExists(atPath: fileURL.path) else {
return nil
}
do {
let fileContents = try Data(contentsOf: fileURL)
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 writeToDisk() {
do {
let data = try JSONEncoder().encode(allPlayers)
try data.write(to: playerDataURL())
} catch let error as NSError {
NSLog("Error reading file: \(error.localizedDescription)")
}
}
func changeMade() {
writeToDisk()
}
func playerList(position: String) -> [Data] {
return allPlayers.filter({ $0.position == position })
}
init() {
allPlayers = [PlayerData]()
if let playerList = readFromDisk() {
allPlayers = playerList
} else {
allPlayers = [
Data(num: 1, name: "Patrick Mahomes", team: "KC", position: "QB"),
Data(num: 2, name: "Deshaun Watson", team: "HOU", position: "QB"),
Data(num: 3, name: "Aaron Rodgers", team: "GB", position: "QB"),
Data(num: 4, name: "Matt Ryan", team: "ATL", position: "QB"),
Data(num: 5, name: "Baker Mayfield", team: "CLE", position: "QB"),
Data(num: 6, name: "Carson Wentz", team: "PHI", position: "QB"),
Data(num: 7, name: "Jared Goff", team: "LAR", position: "QB"),
Data(num: 8, name: "Cam Newton", team: "CAR", position: "QB"),
Data(num: 9, name: "Andrew Luck", team: "IND", position: "QB"),
Data(num: 10, name: "Drew Brees", team: "NO", position: "QB"),
Data(num: 11, name: "Ben Roethlisberger", team: "PIT", position: "QB"),
Data(num: 12, name: "Dak Prescott", team: "DAL", position: "QB"),
Data(num: 13, name: "Russell Wilson", team: "SEA", position: "QB"),
Data(num: 14, name: "Kyler Murray", team: "ARI", position: "QB"),
Data(num: 15, name: "Tom Brady", team: "NE", position: "QB"),
Data(num: 16, name: "Lamar Jackson", team: "BAL", position: "QB"),
Data(num: 17, name: "Mitchell Trubisky", team: "CHI", position: "QB"),
Data(num: 18, name: "Jameis Winston", team: "TB", position: "QB"),
Data(num: 19, name: "Philip Rivers", team: "LAC", position: "QB"),
Data(num: 20, name: "Kirk Cousins", team: "MIN", position: "QB"),
Data(num: 21, name: "Derek Carr", team: "OAK", position: "QB"),
Data(num: 22, name: "Sam Darnold", team: "NYJ", position: "QB"),
Data(num: 23, name: "Josh Allen", team: "BUF", position: "QB"),
Data(num: 24, name: "Matthew Stafford", team: "DET", position: "QB"),
Data(num: 25, name: "Marcus Mariota", team: "TEN", position: "QB"),
Data(num: 26, name: "Jimmy Garoppolo", team: "SF", position: "QB"),
Data(num: 27, name: "Andy Dalton", team: "CIN", position: "QB"),
Data(num: 28, name: "Eli Manning", team: "NYG", position: "QB"),
Data(num: 29, name: "Nick Foles", team: "JAC", position: "QB"),
Data(num: 30, name: "Joe Flacco", team: "DEN", position: "QB"),
// Running Backs
Data(num: 1, name: "Saquon Barkley", team: "NYG", position: "RB"),
Data(num: 2, name: "Christian McCaffrey", team: "CAR", position: "RB"),
Data(num: 3, name: "Alvin Kamara", team: "NO", position: "RB"),
Data(num: 4, name: "Ezekiel Elliott", team: "DAL", position: "RB"),
Data(num: 5, name: "David Johnson", team: "ARI", position: "RB"),
Data(num: 6, name: "Le'Veon Bell", team: "NYJ", position: "RB"),
Data(num: 7, name: "Todd Gurley II", team: "LAR", position: "RB"),
Data(num: 8, name: "Kerryon Johnson", team: "DET", position: "RB"),
Data(num: 9, name: "Joe Mixon", team: "CIN", position: "RB"),
Data(num: 10, name: "James Conner", team: "PIT", position: "RB"),
Data(num: 11, name: "Dalvin Cook", team: "MIN", position: "RB"),
Data(num: 12, name: "Nick Chubb", team: "CLE", position: "RB"),
Data(num: 13, name: "Leonard Fournette", team: "JAC", position: "RB"),
Data(num: 14, name: "Josh Jacobs", team: "OAK", position: "RB"),
Data(num: 15, name: "Devonta Freeman", team: "ATL", position: "RB"),
Data(num: 16, name: "Melvin Gordon", team: "LAC", position: "RB"),
Data(num: 17, name: "Derrick Henry", team: "TEN", position: "RB"),
Data(num: 18, name: "Chris Carson", team: "SEA", position: "RB"),
Data(num: 19, name: "Marlon Mack", team: "IND", position: "RB"),
Data(num: 20, name: "Aaron Jones", team: "GB", position: "RB"),
Data(num: 21, name: "Damien Williams", team: "KC", position: "RB"),
Data(num: 22, name: "Mark Ingram II", team: "BAL", position: "RB"),
Data(num: 23, name: "James White", team: "NE", position: "RB"),
Data(num: 24, name: "Sony Michel", team: "NE", position: "RB"),
Data(num: 25, name: "Kenyan Drake", team: "MIA", position: "RB"),
Data(num: 26, name: "David Montgomery", team: "CHI", position: "RB"),
Data(num: 27, name: "Tarik Cohen", team: "CHI", position: "RB"),
Data(num: 28, name: "Phillip Lindsay", team: "DEN", position: "RB"),
Data(num: 29, name: "Derrius Guice", team: "WAS", position: "RB"),
Data(num: 30, name: "Lamar Miller", team: "HOU", position: "RB"),
Data(num: 31, name: "Tevin Coleman", team: "SF", position: "RB"),
Data(num: 32, name: "Miles Sanders", team: "PHI", position: "RB"),
Data(num: 33, name: "Austin Ekeler", team: "LAC", position: "RB"),
Data(num: 34, name: "Duke Johnson Jr.", team: "HOU", position: "RB"),
Data(num: 35, name: "Royce Freeman", team: "DEN", position: "RB"),
Data(num: 36, name: "Rashaad Penny", team: "SEA", position: "RB"),
Data(num: 37, name: "Nyheim Hines", team: "IND", position: "RB"),
Data(num: 38, name: "Peyton Barber", team: "TB", position: "RB"),
Data(num: 39, name: "Latavius Murray", team: "NO", position: "RB"),
Data(num: 40, name: "Jordan Howard", team: "PHI", position: "RB"),
Data(num: 41, name: "Matt Breida", team: "SF", position: "RB"),
Data(num: 42, name: "LeSean McCoy", team: "BUF", position: "RB"),
Data(num: 43, name: "Darrell Henderson", team: "LAR", position: "RB"),
Data(num: 44, name: "Darwin Thompson", team: "KC", position: "RB"),
Data(num: 45, name: "Ronald Jones", team: "TB", position: "RB"),
Data(num: 46, name: "Devin Singletary", team: "BUF", position: "RB"),
Data(num: 47, name: "Jalen Richard", team: "OAK", position: "RB"),
Data(num: 48, name: "Dion Lewis", team: "TEN", position: "RB"),
Data(num: 49, name: "Giovani Bernard", team: "CIN", position: "RB"),
Data(num: 50, name: "Kalen Ballage", team: "MIA", position: "RB"),
]
}
}
}