gpt4 book ai didi

ios - 过滤字典

转载 作者:行者123 更新时间:2023-11-28 11:53:43 24 4
gpt4 key购买 nike

struct FAQList: Codable {
let valid: Bool?
let message: String?
let faqs: [String: [FAQ]]?
}

struct FAQ: Codable {
let name, question, answer: String?
}

是我的数据模型。

{
"valid": true,
"message": "Faqs fetched successfully!",
"faqs": {
"FAQ8": [
{
"name": "FAQ8",
"question": "Question 2",
"answer": "Answer 2"
},
{
"name": "FAQ8",
"question": "Question 3",
"answer": "Answer 3"
},
{
"name": "FAQ8",
"question": "Question 4",
"answer": "Answer 4"
},
{
"name": "FAQ8",
"question": "Question 5",
"answer": "Answer 5"
},
{
"name": "FAQ8",
"question": "Question 6",
"answer": "Answer 6"
},
{
"name": "FAQ8",
"question": "Question 7",
"answer": "Answer 7"
},
{
"name": "FAQ8",
"question": "Question 8",
"answer": "Answer 8"
},
{
"name": "FAQ8",
"question": "sdrt",
"answer": ""
}
],
"abcs1sd122": [
{
"name": "abcs1sd122",
"question": "sD",
"answer": "ssaf"
}
],
"ASDFG": [
{
"name": "ASDFG",
"question": "kalpana",
"answer": "UI developer"
},
{
"name": "ASDFG",
"question": "asDFGHJWQ",
"answer": "ERTYU"
},
{
"name": "ASDFG",
"question": "sdede",
"answer": "ERTYU"
}
],
"test": [
{
"name": "test",
"question": "ASdfg",
"answer": "AASDFTRYU"
},
{
"name": "test",
"question": "ASDRFT",
"answer": "Q"
},
{
"name": "test",
"question": "SADRTY",
"answer": "WQEW3E45R6"
},
{
"name": "test",
"question": "zxcVG",
"answer": "Sadfgh"
},
{
"name": "test",
"question": "saesrtyu",
"answer": "3q5w46e567890"
}
]
}
}

是我的回应。

现在。我需要过滤这个“常见问题解答”字典以形成基于键的主题列表数组。

像 'FAQ8' 或 'abcs1sd122' 是一个由所有问题组成的数组,这些问题的 'name' 分别为 'FAQ8' 或 'abcs1sd122'。

我用过这个,

 let FAQ8 = self.viewModel.faqsList.faqs?.filter({ $0.key == "FAQ8" })

但它显示错误。

请建议实现此目标的最佳方法。

最佳答案

如果你想要所有名称为 FAQ8 的常见问题,你为什么不简单地写这个?

let faqs = self.viewModel.faqsList.faqs?["FAQ8"]

关于ios - 过滤字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51479892/

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