如何检查数组是否包含nil值? [英] How do you check an array to see if it contains a nil value?
本文介绍了如何检查数组是否包含nil值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个空数组,其中充满了nil,我将填充一个充满int的数组,并想知道它何时不再包含nil值。
I have an empty array full of nil, I will be populating the array full of ints and want to see when it no longer contains a nil value.
I需要此检查以查看是否存在nil,以便可以添加int(如果存在),并且如果没有nil值,则停止游戏。
I need this check to see if there is nil so I can add ints if there is, and if there is no nil values then stop the game.
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}) {
另一种方式:
Another way:
if smallestArr.flatMap{$0}.count == smallestArr.count {
这篇关于如何检查数组是否包含nil值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文