我想比较两个模型mainArray
和resultArray
。如果发现category_name
和sub_category_name
相同,则将mainArray
is_selected
更改为true。我怎样才能做到这一点呢?他们都是模特。
var mainArray : [Item] = [
{
"category_name": "Wellness",
"sub_category": [
{
"sub_category_name": "Health",
"is_selected": false
},
{
"sub_category_name": "Psychedelics",
"is_selected": false
},
{
"sub_category_name": "Meditation",
"is_selected": false
},
{
"sub_category_name": "Nutrition",
"is_selected": false
},
{
"sub_category_name": "Outdoors",
"is_selected": false
},
{
"sub_category_name": "Medicine",
"is_selected": false
},
{
"sub_category_name": "Mindfulness",
"is_selected": false
},
{
"sub_category_name": "Fitness",
"is_selected": false
},
{
"sub_category_name": "Weights",
"is_selected": false
},
{
"sub_category_name": "Veganism",
"is_selected": false
}
]
},
{
"category_name": "Hustle",
"sub_category": [
{
"sub_category_name": "Tik Tok",
"is_selected": false
},
{
"sub_category_name": "ClubHouse",
"is_selected": false
},
{
"sub_category_name": "Stocks",
"is_selected": false
},
{
"sub_category_name": "Networking",
"is_selected": false
},
{
"sub_category_name": "Small business",
"is_selected": false
},
{
"sub_category_name": "Instagram",
"is_selected": false
},
{
"sub_category_name": "Enterpreneurship",
"is_selected": false
},
{
"sub_category_name": "Real Estate",
"is_selected": false
},
{
"sub_category_name": "Pitch practice",
"is_selected": false
}
]
}
]
var resultArray : [Item] = [
{
"category_name": "Wellness",
"sub_category": [
{
"sub_category_name": "Psychedelics"
},
{
"sub_category_name": "Fitness"
}
]
},
{
"category_name": "Hustle",
"sub_category": [
{
"sub_category_name": "ClubHouse"
}
]
}
]
这是项目模型
// MARK: - Item
class Item: Codable {
let category_name: String
let sub_category: [SubCategory]
enum CodingKeys: String, CodingKey {
case category_name = "category_name"
case sub_category = "sub_category"
}
init(categoryName: String, subCategory: [SubCategory]) {
self.category_name = categoryName
self.sub_category = subCategory
}
}
首先
var mainArray : [Item] = [{
没有意义,因为数据是一个JSON字符串,而不是一个Item
数组。
其次,就地更新数组有点棘手,因为你可能不得不处理值语义。
这个建议甚至适用于结构体。我将名称改为更符合swift的名称
let main = """
[
{
"category_name": "Wellness",
"sub_category": [ ...
let result = """
[
{
"category_name": "Wellness",
"sub_category": [ ...
struct Category: Codable {
let name: String
var subCategories: [SubCategory]
private enum CodingKeys: String, CodingKey {
case name = "category_name"
case subCategories = "sub_category"
}
}
struct SubCategory: Codable {
let name: String
var isSelected = false
private enum CodingKeys: String, CodingKey { case name = "sub_category_name" }
}
func select(main: inout [Category], from result: [Category]) {
result.forEach { category in
guard let mainCategoryIndex = main.firstIndex(where: {$0.name == category.name}) else { return }
for subCategory in category.subCategories {
guard let subCategoryIndex = main[mainCategoryIndex].subCategories.firstIndex(where: {$0.name == subCategory.name}) else { return }
main[mainCategoryIndex].subCategories[subCategoryIndex].isSelected = true
}
}
}
do {
var main = try JSONDecoder().decode([Category].self, from: Data(main.utf8))
let result = try JSONDecoder().decode([Category].self, from: Data(result.utf8))
select(main: &main, from: result)
print(main)
} catch {
print(error)
}