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”的数组是由所有具有“名称”分别为“ FAQ8”或“ abcs1sd122”的问题组成的数组。
我用了这个,
let FAQ8 = self.viewModel.faqsList.faqs?.filter({ $0.key == "FAQ8" })
但显示错误。
请提出实现此目标的最佳方法。
答案 0 :(得分:2)
如果您想让所有常见问题的名字都为FAQ8
,为什么不简单地写这个呢?
let faqs = self.viewModel.faqsList.faqs?["FAQ8"]