ios - 如何检查数组中的所有字段是否包含数据

标签 ios swift loops

我想检查数组以查看是否所有字段都有值,如果所有字段都有值,那么我希望它做一些事情。我的代码确实有效,但它真的很乱。我想知道是否有更简单的方法来做到这一点。

@IBAction func testBtn(sender: AnyObject) {
          self.textData = arrayCellsTextFields.valueForKey("text") as! [String]

    for item in textData {
        if item.isEmpty {


        print("Missing")
            switchKey = true
          // alertviewer will go here
        break


        } else {

        switchKey = false
        }
    }

    if switchKey == false {
        //navigation here
        print("done")

    }

}

最佳答案

你可以用filter函数来做到这一点

if textData.filter({$0.isEmpty}).count > 0 {
  // there is at least one empty item
} else {
  // all items contain data
}

关于ios - 如何检查数组中的所有字段是否包含数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32404413/

相关文章:

ios - 无法使用类型的参数列表调用 animateWithDuration

ios - Textview scrollRangeToVisible 使范围到顶​​部

loops - clojure 中的顽固循环,让不被评估

我不能只打印一个循环吗? - C

java - 迭代集合的数组列表

android - Ionic 4 加载本地资源不适用于 IOS

ios - 在 Swift 中向自定义按钮添加闭包

iphone - 如何使用父 View Controller 启动 uiactionsheet 使其不被剪切?

swift - 关于单例类中定义的常量

android - xamarin 在整个网格列上形成拉伸(stretch)按钮