我试图找出自定义类型的数组是否在2D数组中包含该自定义类型,但收到了以下错误:Cannot convert value of type 'Modifier' to expected argument type '(Modifier) throws -> Bool'
不确定这个代码中出了什么问题:
for x in 0..<tableDataSource.count {
for i in 0..<tableDataSource[x].count {
if(existingModifiers.contains(where: tableDataSource[x][i].mod)){
tableDataSource[x][i].selected = true
}
}
}
existingModifiers是类型为Modifier
的数组tableDataSource是类型为tableElement
的2D数组其中tableElement
包含Modifier
属性
替换:
if(existingModifiers.contains(where: tableDataSource[x][i].mod))
带有:
if(existingModifiers.contains(where: { $0 == tableDataSource[x][i].mod }))
注意
在Swift中,为了更清晰,可以省略if
语句中的括号:
if existingModifiers.contains(where: { $0 == tableDataSource[x][i].mod })