gpt4 book ai didi

ios - 为什么要减去数组中的某个项目而不是添加它?

转载 作者:行者123 更新时间:2023-11-30 12:16:43 24 4
gpt4 key购买 nike

我有一个包含 4 个项目的默认数组。当我尝试从第二个 View 添加项目时,一项会被删除,但应该添加。每当 saveNewScore 被触发时,我应该向数组添加一个新项目。

第一次查看我有这个数组

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

前面的数组有 4 个默认值。后来我有一个 un Wind segue IBAction

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

前面的代码从playerEditor View 获取一个新数组,这是我的第二个 View Controller 。

对于名为 playerEditor第二个 View ,我有以下代码

// 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)
}
}
}

前面的代码将一个新的 Player 添加到 Player 类型的playerData 数组中,该数组从多个 UITextField 获取信息。

排序功能

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
}

最佳答案

在下面的方法中检查 newData 的计数。看起来您要更新到的 playerData 数组有所不同:-

@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()
}

关于ios - 为什么要减去数组中的某个项目而不是添加它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45354144/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com