0

I have a JSON array fetching from url and convert using SwiftyJSON. Now i want to update some value of JSON Array for some functionality.

My JSON is like

[
    {
      "id" : 48845152,
      "studentPhotoTakenCount" : 0,
      "updatedAt" : null,
      "isAttendedToday: false
    },
 {     "id" : 48845153,
      "studentPhotoTakenCount" : 0,
      "updatedAt" : null,
      "isAttendedToday: false
    },
  
  ]

After some action i want to update my JSON Array by filtering id. Like if i have id = 48845152 then only update

{
      "id" : 48845152,
      "studentPhotoTakenCount" : 0,
      "updatedAt" : null,
      "isAttendedToday: false
    }

and finally merge with my JSON Array. So the final result should be

[
    {
      "id" : 48845152,
      "studentPhotoTakenCount" : 0,
      "updatedAt" : null,
      "isAttendedToday: false
    },
 {     "id" : 48845153,
      "studentPhotoTakenCount" : 0,
      "updatedAt" : null,
      "isAttendedToday: false
    },

  ]

My code is like that.

self.studentList.forEach {
                        if let id = $0["id"].int {
                            if id == _studentId {
                                $0["isAttendedToday"] =  true
                                self.filteredStudentList.append($0)
                            }
                            else {
                                self.filteredStudentList.append($0)
                            }
                        }
                    }

Here self.studentList is my JSON. But i get the error saying that

Cannot assign through subscript: '$0' is immutable

Please someOne help me to find out what getting wrong here.

1
  • $0["isAttendedToday"] = true, this line causing the issue Commented Dec 24, 2020 at 16:06

1 Answer 1

2

With this syntax you cannot modify the source array, but you could modify the destination array

self.studentList.forEach {
    self.filteredStudentList.append($0)
    if let id = $0["id"].int, id == _studentId {
       let lastIndex = self.filteredStudentList.count - 1
       self.filteredStudentList[lastIndex]["isAttendedToday"] = true
    }
}

If the source array is supposed to be modified you have to use this

for (index, item) in self.studentList.enumerated() {
    self.filteredStudentList.append(item)
    if let id = item["id"].int, id == _studentId {
       self.studentList[index]["isAttendedToday"] = true
    }
}

In both cases due to value type semantics you have to modify the arrays directly.

PS:

In Swift 5 there is no reason anymore to use SwiftyJSON. Codable is more versatile, more efficient and built-in.

Sign up to request clarification or add additional context in comments.

5 Comments

your first solution got some error saying 'Cannot assign to property: 'last' is a get-only property'. But second method is working. thank you
Please see the edit and be aware that both suggestions do different things.
thank you very much but can you describe why 'Cannot assign through subscript: '$0' is immutable' error occured??
$0 represents the current item in the loop. It's a copy and treated as let constant, therefore immutable.
Thanks again. Actually i am new to swift and learning with working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.