【发布时间】:2026-02-12 13:35:01
【问题描述】:
我正在尝试获取 JSON 值并附加到数组中。在这里,下面的代码add_user_product有机会来null。如果是null则需要在数组中追加null,如果不是null也需要存储ID。
我正在尝试获得类似的输出 - [10,null,12,13,null,……]
// add_user_products & If add_user_product == null need to store null otherwise add_user_product ["id"]
if let add_user_product = fields[“add_user_product"] as? [String : Any] {
let add_id = add_user_product["id"] as! Int
self.addlistIDData.append(add_id)
}
else {
//print("Failure")
}
在我的示例回复下方
{
"students":[
{
"grade":0,
"add_user_product":
{
"id":10
}
},
{
"grade":1,
"add_user_product":null
},
{
"grade":2,
"add_user_product":
{
"id":11
}
}
]
}
Expected output: [10,null,11,......] //This array I am going to use Tableview cell
【问题讨论】:
-
如果没有
add_user_product,tableviewcell中会显示什么数据?