请考虑以下事项:
let isHaveSn = model.positions.contains {$0.assortment?.isSerialTrackable == true}
guard isHaveSn else {
return
}
isHaveSn产品Bool,所以我检查Bool在后卫,一切正常。但我想这样简化:
guard model.positions.contains {$0.assortment?.isSerialTrackable == true} else {
return
}
它产生了几个错误,其中一些错误:期望表达式或连续语句应该用;
分隔但我只是复制右侧:
let isHaveSn = model.positions.contains {$0.assortment?.isSerialTrackable == true}
为什么我的代码无法编译?
答案 0 :(得分:3)
正确的语法是:
guard model.positions.contains(where: {$0.assortment?.isSerialTrackable == true}) else {
return
}
实际上,在swift-evolution中有一个提议,允许在被拒绝的保护语句中进行尾随闭包。您可以阅读more about it here