我确信这很简单。但我不确定发生了什么。我快速休息了一下,然后用Swift三回到它。
我希望点击播放按钮时isPlaying
为真,我currentLineup[0]
中的人更新了他们的值。他们在调用函数时似乎做了什么。但是,当我致电currentLieneup[0][0]
时,值似乎又回到isPlaying: false, timeCameOn: 0
,如日志所示?
我有以下内容:
var currentLineup: [[Player]] = []
@IBAction func playPause(_ sender: UIButton) {
// change icon to play or pause
isPlaying = isPlaying ? false : true
// set player.timeCameOn to current time or back to 0
updatePlayerGameStartTime(gameInProgress: isPlaying)
print(currentLineup[0][0])
}
// When play button pressed, make sure players timeCameOn updated
func updatePlayerGameStartTime(gameInProgress: Bool){
if gameInProgress {
for var player in currentLineup[0]{ // Playing
player.isPlaying = true
player.timeCameOn = Int(Date().timeIntervalSince1970)
print(player)
}
for var player in currentLineup[1]{ // Subs
player.isPlaying = false
player.timeCameOn = 0
}
}
}
我在日志中注意到以下内容:
Player(name: "Player 1", isPlaying: true, timeCameOn: 1488304289, currentPlayedTime: 0)
Player(name: "Player 2", isPlaying: true, timeCameOn: 1488304289, currentPlayedTime: 0)
Player(name: "Player 3", isPlaying: true, timeCameOn: 1488304289, currentPlayedTime: 0)
Player(name: "Player 4", isPlaying: true, timeCameOn: 1488304289, currentPlayedTime: 0)
Player(name: "Player 1", isPlaying: false, timeCameOn: 0, currentPlayedTime: 0)
更新
我通过在viewdidload()中调用一个名为currentLineup
的函数来设置初始createInitialCurrentPlayingList()
。
createInitialCurrentPlayingList(listOfPlayers: [Player]){
for player in listOfPlayers {
if startingLineup.count < 4 {
startingLineup.append(player)
} else {
startingSubs.append(player)
}
}
currentLineup.insert(startingLineup, at:0)
currentLineup.insert(startingSubs, at: 1)
}
// declarations of vars
startingLineup: [Player] = []
startingSubs: [Player] = []
currentLineup: [[Player]] = []
答案 0 :(得分:1)
这很可能是价值类型问题
请尝试此操作,它会将子阵列分配回currentLineup
。
var firstLineup = currentLineup[0]
for player in firstLineup { // Playing
player.isPlaying = true
player.timeCameOn = Int(Date().timeIntervalSince1970)
print(player)
}
currentLineup[0] = firstLineup