Matthew Xavier Jacome Matthew Xavier Jacome - 1 year ago 56
iOS Question

Why is it subtracting an Item in the array instead of adding it?

I have a default array with 4 items. When I try to add a new item from a second View one item gets deleted, but it should be added. Whenever saveNewScore is triggered I should add a new Item to the array.

For the first View
I have this array

var playerData: [Player] = Player.createDefaultData()


The previous array has 4 default values.
Later on I have a un wind segue IBAction

@IBAction func newPlayerData(segue: UIStoryboardSegue) {
let editor = segue.source as! playerEditor
if let newData = editor.playerData {
playerData = newData
}
displayPlayerLeaderboard()
}


The previous code gets a new array from the playerEditor view, which is my second view controller.

For the Second View called playerEditor I have the following code

// Player Array
var playerData: [Player]?
// Func to add new player to playerData
@IBAction func saveNewScore() {
if let name = nameField.text, let hometown = hometownField.text, let state = stateField.text, let score = scoreField.text, let date = dateField.text {
if name.isEmpty == false && hometown.isEmpty == false && state.isEmpty == false && score.isEmpty == false && date.isEmpty == false {
let thisScore = Int(score) ?? 300
if thisScore >= 300 || thisScore < 0 {
let alert = UIAlertController(title: "Invalid Score", message: "A Player score must between the values of 0 and 300.", preferredStyle: .alert)
let okButton = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(okButton)
present(alert, animated: true)
} else if let savedPlayer = playerID, let savedData = playerData {
for (offset,player) in savedData.enumerated() {
if player.uniqueID == savedplayer {
player.name = name
player.town = hometown
player.state = state
player.score = thisScore
player.date = date
player.Data?[offset] = player
}
}
dismiss(animated: true, completion: nil)
} else {
let newPlayer = Player(name: name, town: hometown, state: state, score: thisScore, date: date, uniqueID: largestID + 1)
playerData?.append(newPlayer)
dismiss(animated: true, completion: nil)
}
} else {
let alert = UIAlertController(title: "Invalid Data", message: "Ensure that all text fields have data in them before proceeding.", preferredStyle: .alert)
let okButton = UIAlertAction(title: "OK", style: .default, handler: nil)
// add action to alert controller
alert.addAction(okButton)
present(alert, animated: true)
}
}
}


The previous code adds a new Player to the playerData array of type Player that gets the information from multiple UITextFields.

Sort Function

func sortData(data: [Player]) -> [Player] {
var newData: [Player] = []
for (offset,player) in data.enumerated() {
if offset == 1 {
newData += [player]
} else {
for (offset,newPlayer) in newData.enumerated() {
if player.score >= newPlayer.score {
newData.insert(player, at: offset)
break
} else if offset == (newData.count - 1) {
newData.append(player)
}
}
}

}
return newData
}

Answer Source

Check the count of newData in below method. Seems like its different which you are updating to playerData array:-

@IBAction func newPlayerData(segue: UIStoryboardSegue) {
        let editor = segue.source as! playerEditor
        if let newData = editor.playerData {
        //Check the count of newData here
            playerData = newData
        }
        displayPlayerLeaderboard()
    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download