在swift中的自定义类型数组上使用.contents



我试图找出自定义类型的数组是否在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 })

最新更新