ios - 对象数组还有进一步的内部对象数组

标签 ios arrays swift

我的类(class)

class ScoreModel {
    var playerId: Int?
    var holeScores: [HoleScore]?
}

其他类

class HoleScore {
    var holeScore: Int?
}

我有这些类,其中一个是 ScoreModel 类,它可以具有 HoleScore 的对象数组

let scoreList = [ScoreModel]()
scoreList[0].holeScores![0].holeScore = 3

当我更新或更改 scoreList[0].holeScores[0]holeScore 时,它会更改所有 scoreList[forAllIndexes].holeScores[0 ]。我只想更改外部数组给定索引的内部数组 prams,但它会在更新时更改所有 holeScore 值。

最佳答案

这会附加相同的对象,因此其中一个对象的更改会反射(reflect)到其他对象

var item = HoleScore()

for i in 0...5
{
    item. holeScore = i

    scoreList[0].holeScores.append(item)
}

//

这会附加不同的对象,因此其中一个对象的更改不会反射(reflect)到其他对象

for i in 0...5
{
    var item = HoleScore()

    item. holeScore = i

    scoreList[0].holeScores.append(item)
}

关于ios - 对象数组还有进一步的内部对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49996646/

相关文章:

ios - subview 不滚动,但滚动条快速滚动

c++ - 无法解决将二维数组传递给函数的外部问题

swift - 在菜单中使用 NavigationLink (SwiftUI)

ios - AEXMLDocument loadXMLData() 在 Swift 中不起作用

ios - 如何在 cellforrowatindexpath 之后更新自定义表格单元格中的 UIButton 图像?

ios - 2节点 Sprite 套件的异步移动

ios - 在 iOS 7 中查看 Core Data 创建的 sqlite 文件时出现问题

java - Jni jintArray 参数错误

javascript - 将每个列表数组包装在一个标签中

ios - Swift - 在后台打开和关闭位置以节省电池