我试图在两个不同的数组中匹配属性flavour
。
阵列一:
var myIcecreams = [
Icecream(color: "yellow", flavour: "banana"),
Icecream(color: "green", flavour: "mint"),
Icecream(color: "white", flavour: "banana"),
Icecream(color: "brown", flavour: "chocolate")
]
阵列二:
var shopIcecreams = [
Icecream(color: "red", flavour: "strawberry"),
Icecream(color: "purple", flavour: "blueberry"),
Icecream(color: "green", flavour: "pistachio"),
Icecream(color: "white", flavour: "chocolate")
]
如何创建一个新数组,包含shopIcecreams
中的所有元素,除了属性flavour
相互匹配的元素?冰淇淋中有一个匹配的chocolate
作为风味,应该在新数组中删除。
我试过了,没有运气:
let filteredIcecreams = shopIcecreams.filter { newIcecream in
myIcecreams.contains { myIcecream in
newIcecream.flavour != myIcecream.flavour
}
}
我做错了什么?我毫无头绪。
冰淇淋结构:
struct Icecream: Encodable, Decodable {
var color: String
var flavour: String
}
2条答案
按热度按时间k10s72fa1#
这是我认为你想要的:
wf82jlnq2#
Click Here to see the output
输出:
冰淇淋(颜色:“红色”,风味:“草莓”)冰淇淋(颜色:“紫”,味:“蓝莓”)冰淇淋(颜色:“绿色”,风味:“开心果”)