【发布时间】:2020-08-19 09:11:18
【问题描述】:
我正在尝试更改模型中的值或更新值,但由于某种未知原因,它没有更新值。
我已经通过匹配 id 准确地找到了该行,但之后该值没有更新。
我想在Item 模型中更新bgColor。
减速:var AppDetailData : AppointmentDetail?
我的代码:
for var i in AppDetailData?.sectionList ?? [] {
for j in i.items ?? [] {
if let row = i.items?.firstIndex(where: {$0.unitId == id}) {
i.items?[row].bgColor = "yellow"
}
}
}
JSON 模型:
struct AppointmentDetail : Codable {
let projectId : Int?
let projectName : String?
let projectNo : String?
let projectType : String?
let sectionList : [SectionList]?
}
struct SectionList : Codable {
let title : String?
var sectionId: Int?
var items : [Item]?
}
struct Item : Codable {
var bgColor : String?
var unitId : Int?
var latitude : Double?
var longitude : Double?
}
【问题讨论】:
-
为什么所有的东西都是可选的?这是你的 json 还是来自一些外部的 rest api?
-
@JoakimDanielson - 我的 Json,是的,我会设置它。
标签: ios arrays swift model codable