0

I have a custom model like bellow:

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?
}

And:

struct SectionModel {
    var expand:Bool
    var title:String
    var content:[ContentModel]
}

How can I update a specific item from above model?

I am using from bellow code:

for resRoot in  displayListCourseFilter{
    for resSub in resRoot.content{
        if pId == resSub.parentID{
            resSub.selectAll = "true"
            resSub.isSelected = "true"
        }
    }
}

But say me:

Cannot assign to property: 'resSub' is a 'let' constant

NOTICE: All of my variables are var.

jo jo
  • 1,758
  • 3
  • 20
  • 34
  • “All of my variables are var” No they are not. resSub and resRoot are let. And even if they were var they are still _copies_, so changing one won’t help you – matt Oct 06 '19 at 14:29
  • Here is var displayListCourseFilter: [SectionModel] = [] and all of items are var, resolve my problem with response of Sh_Khan. Thank's a lot – jo jo Oct 06 '19 at 14:37
  • That was also the answer in the duplicate https://stackoverflow.com/questions/54014345/how-do-people-deal-with-iterating-a-swift-struct-value-type-property. – matt Oct 06 '19 at 14:44

1 Answers1

1

With struct

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

or make it a class and it will compile

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87