【发布时间】:2021-04-03 00:59:09
【问题描述】:
我有一个从 url 获取并使用 SwiftyJSON 转换的 JSON 数组。现在我想为某些功能更新一些 JSON 数组的值。
我的 JSON 是这样的
[
{
"id" : 48845152,
"studentPhotoTakenCount" : 0,
"updatedAt" : null,
"isAttendedToday: false
},
{ "id" : 48845153,
"studentPhotoTakenCount" : 0,
"updatedAt" : null,
"isAttendedToday: false
},
]
经过一些操作后,我想通过过滤 id 来更新我的 JSON 数组。 就像如果我有 id = 48845152 然后只更新
{
"id" : 48845152,
"studentPhotoTakenCount" : 0,
"updatedAt" : null,
"isAttendedToday: false
}
最后与我的 JSON 数组合并。所以最终的结果应该是
[
{
"id" : 48845152,
"studentPhotoTakenCount" : 0,
"updatedAt" : null,
"isAttendedToday: false
},
{ "id" : 48845153,
"studentPhotoTakenCount" : 0,
"updatedAt" : null,
"isAttendedToday: false
},
]
我的代码是这样的。
self.studentList.forEach {
if let id = $0["id"].int {
if id == _studentId {
$0["isAttendedToday"] = true
self.filteredStudentList.append($0)
}
else {
self.filteredStudentList.append($0)
}
}
}
self.studentList 是我的 JSON。但我收到错误提示
不能通过下标赋值:'$0' 是不可变的
请有人帮我找出这里出了什么问题。
【问题讨论】:
-
$0["isAttendedToday"] = true,此行导致问题
标签: ios json swift swift5 swifty-json