Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 更新/更改自定义对象的数组值-swift_Arrays_Swift - Fatal编程技术网

Arrays 更新/更改自定义对象的数组值-swift

Arrays 更新/更改自定义对象的数组值-swift,arrays,swift,Arrays,Swift,我有一个定制的型号,如下所示: struct ContentModel { var id: Int64? var parentID: Int64? var subject: String? var langId: String? var isSelected: String? var identifier: String? var title: String? var count: Int64? var selectAll:

我有一个定制的
型号
,如下所示:

struct ContentModel {
    var id: Int64?
    var parentID: Int64?
    var subject: String?
    var langId: String?
    var isSelected: String?
    var identifier: String?
    var title: String?
    var count: Int64?
    var selectAll: String?
    var textSelectAll: String?
}
以及:

struct SectionModel {
    var expand:Bool
    var title:String
    var content:[ContentModel]
}
如何从上述模型更新特定项目

我正在使用以下代码:

for resRoot in  displayListCourseFilter{
    for resSub in resRoot.content{
        if pId == resSub.parentID{
            resSub.selectAll = "true"
            resSub.isSelected = "true"
        }
    }
}
但请告诉我:

Cannot assign to property: 'resSub' is a 'let' constant
注意:我所有的变量都是带有struct的
var

for i in displayListCourseFilter.indices {
   for j in displayListCourseFilter[i].content.indices  { 
        var item = displayListCourseFilter[i].content[j]
        if pId ==  item.parentID {
             item.selectAll = "true"
             item.isSelected = "true"
             displayListCourseFilter[i].content[j] = item
        }
    }
}

或者让它成为一个类,它将编译

@shu Khan。Get me无法为属性赋值:“resRoot”是一个“let”常量。“我所有的变量都是var”不,它们不是。resSub和resRoot是出租的。即使它们是var,它们仍然是副本,因此更改一个对您没有帮助这里是var displayListCourseFilter:[SectionModel]=[]并且所有项目都是var,请使用Shu Khan的响应解决我的问题。谢谢,这也是答案的副本。