我有一个全为 nil 的空数组,我将填充全为 int 的数组,并想看看它何时不再包含 nil 值。
我需要检查以查看是否有 nil,如果有我可以添加整数,如果没有 nil 值则停止游戏。
var smallestArr = [Int?](count: 25, repeatedValue: nil)
if smallestArr.contains(nil){
//add ints until it doesn't contain anymore nil
}else{
//end game
}
最佳答案
代替
if smallestArr.contains(nil) {
放这个:
if smallestArr.contains{$0 == nil} {
swift 4:
if smallestArr.contains(where: {$0 == nil}) {
另一种方式:
if smallestArr.flatMap{$0}.count == smallestArr.count {
关于arrays - 如何检查数组以查看它是否包含 nil 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34427759/