我试图将字典附加到我的数组,但我不知道如何去做,即使在阅读了一堆线程之后。
我有这个结构体:
// MARK: - Main
struct Main: Codable {
var objects: [Object]
}
// MARK: - Object
struct Object: Codable {
let id:Int
let url: String
var displayname: String
let inventory: [Inventory]
let media: [Media]
}
和这个结构体的变量:
var searchresult: Main?
假设我想将下面的字典附加到上面的struct/variable object数组中:
{
"id":12994,
"url":"www.google.com",
"displayname":"google",
"inventory":[],
"media":[]
}
我该怎么做呢?我一直在尝试阅读其他线程,但并没有真正理解太多:(我希望最终的结果是,对象数组被一堆不同的字典填充,然后我可以搜索,如下所示:
"meta":{
"status":"OK",
"count":5,
"offset":0,
"totalcount":5
},
"objects":[
{
"id":8918,
"url":"example",
"displayname":"example",
"inventory":[],
"media":[]
},
{
"id":12994,
"url":"example",
"displayname":"example",
"inventory":[],
"media":[]
},
{
"id":19559,
"url":"example",
"displayname":"example",
"inventory":[],
"media":[]
},
{
"id":21100,
"url":"example",
"displayname":"example",
"inventory":[],
"media":[]
},
{
"id":22250,
"url":"Example",
"displayname":"Example",
"inventory":[],
"media":[]
}
]
尝试searchresult?.objects.append(your_dictionary)
。然后你可以得到searchresult?.objects
。并且确保Inventory
和Media
类/结构也是可编码的。