arrays - 如何检查数组以查看它是否包含 nil 值?

标签 arrays swift

我有一个全为 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/

相关文章:

java - 存储值不起作用?

c++ - 将文件内容读入动态分配的 char* 数组——我可以改为读入 std::string 吗?

html - 将字符串编码为 HTML 字符串 Swift 3

ios - 在 RxSwift 中从 ViewModel 获取字符串到 View

java - 如何在不创建新适配器等的情况下更新ListView中的ListItems

python - 如何获得数组中不存在的最小正整数

arrays - Elasticsearch:设置字段中数组元素的数量

swift - 使 Cocoa NSWindow 半透明隐藏 Storyboard元素

ios - 在 AppDelegate 中对初始 ViewController 使用强委托(delegate)

ios - 计算属性中是否可以保留循环?