gpt4 book ai didi

ios - swift 中的嵌套重复循环

转载 作者:搜寻专家 更新时间:2023-10-31 22:39:19 24 4
gpt4 key购买 nike

我试图在 swift 中解析一个嵌套的迭代循环我从 Web 服务获得以下格式的响应

{
"categories": [{
"name": "Default Category",
"id": "default_category",
"children": [{
"uuid": "783f491fef5041438fb7a2c3bf6a3650",
"name": "Accessories",
"children": [{
"uuid": "d21b4491ff784a9bae88de279b99fac3",
"name": "All Accessories",
"children": [{
"uuid": "2b1a23c4107844ad8a7afc1b324d0ffd",
"name": "Belts",
"children": [{
"uuid": "2b1a23c4107844ad8a7afc1b324d0ffd",
"name": "Belts",
"children": []

},
{
"uuid": "2b1a23c4107844ad8a7afc1b324d0ffd",
"name": "Belts",
"children": []
}
]
},
{
"uuid": "a1c2a64c36c2461cad3d5f850e4fd0f5",
"name": "Hats",
"children": []
},
{
"uuid": "8f26bc764b8342feaa0cb7f3b96adcae",
"name": "Scarves",
"children": []
},
{
"uuid": "aa1116d1a0254ecea836cc6b32eeb9e0",
"name": "Sunglasses",
"children": []
},
{
"uuid": "9d7033233e8f47eaa69eb1aaf2e98cdd",
"name": "Watches",
"children": []
}
]
}]
}],
"uuid": "6a23415771064e7aaad59f84f8113561"
}]
}

在类别中,有一个“children”键,它又可以包含另一个 child 等等。我想在子键内不断循环,直到子键为空,然后将最后一个子键插入数据库。

下面是我完成的代码

     for currentCategory in mainCategories {


// guard against if there are child categories
guard var children = currentCategory.children, children.count > 0 else {
// Save the context
self.coreData.saveStore()
continue
}
for thisChildCategory in children {

if thisChildCategory.children?.count > 0 {
for innerChildCategory in thisChildCategory.children! {
print("innerChildCategory name \(String(describing: innerChildCategory.name))")
}
}

if let child = thisChildCategory.children {
children = child
}

// Create new object
if let currentChildCategory = self.coreData.insertNewObject(CoreDataEntities.BijouCategories.rawValue,
keyValues: ["id" : thisChildCategory.id! as Optional<AnyObject>,
"uuid" : thisChildCategory.uuid as Optional<AnyObject>,
"name" : thisChildCategory.name! as Optional<AnyObject>,
"gender" : thisChildCategory.gender as Optional<AnyObject>!,
"active" : NSNumber(value: false)]) as? BijouCategories {

// Set as parent category
currentChildCategory.parentCategory = parentCategory


// Save the context
self.coreData.saveStore()

}
}

}

但这并没有保存数据库中的所有最后一个子类别。

最佳答案

swift 4

您应该让 Swift 4 的 codable 为您完成这项工作。

您可以使用以下类作为结构,但我发现如果您计划编辑数据,使用类会更好。

class Categories: Codable {
var categories: [CategoryItems]
}

class CategoryItems: Codable {
var name: String?
var id: String?
var uuid: String?
var children: [CategoryItems]?

required init(from decoder: Decoder) throws {
var container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decodeIfPresent(String.self, forKey: CodingKeys.name)
id = try container.decodeIfPresent(String.self, forKey: CodingKeys.id)
uuid = try container.decodeIfPresent(String.self, forKey: CodingKeys.uuid)
children = try container.decodeIfPresent([CategoryItems].self, forKey: CodingKeys.children)
if children != nil, children!.count == 0 {
children = nil
}
}

您可以在此处看到我们添加了创建具有 CategoryItems 数组的根级类“Categories”。 CategoryItems 中包含所有可能的值,但数组中的每个项目可能具有也可能不具有所有可能的值,因此它们是可选的。重要的是 child ,这是可选的。然后在所需的 init 中,如果在解码时键值对可用,我们只设置可选值。如果项目为零,我还将子项设置为 nil,这是可选的,但在稍后执行 if 语句时会有所帮助。

然后使用以下代码使用这些可编码类解码您的 json。

func decode(jsonData data: Data) {
let decoder = JSONDecoder()
do {
let decoded = try decoder.decode(Categories.self, from: data)
}
catch let error as NSError {
print("JSON Decode error = ", error)
}
}

如果你想做一个快速测试,看看你是否达到了我所做的更深层次的 child 水平,你可以简单地在解码变量上运行以下命令。

for i in decoded.categories.first!.children!.first!.children!.first!.children!.first!.children! {
print(i.name)
print(i.uuid)
}

关于ios - swift 中的嵌套重复循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50942925/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com