首页 > 解决方案 > 如果来自firebase的所有子值都为真(swift),我想写条件

问题描述

我是 firebase 的新手,现在我想写下如果从 firebase 收到的所有值都是真的,我想做点什么。

这是我的数据库实时

- date
    -Friday: true
    -Monday: true
    -Saturday: true
    -Sunday: true
    -Thursday: true
    -Tuesday: true
    -Wednesday: true

这是我的代码

_ = Database.database().reference().child("Info/date")
            .observeSingleEvent(of: .value, with: { (snapshot) in

                if snapshot.exists() {

                    if let dictionaryArray = snapshot.value as? [String: Bool] {
                        print(dictionaryArray.values)
                        if dictionaryArray.values == true {
                            print("All is true")
                        }else{
                            print("All is not true")
                        }
                    }
                }else{
                }
            })

但它在这一行有一个错误“if dictionaryArray.values == true {”

它表明

Binary operator '==' cannot be applied to operands of type 'Dictionary<String, Bool>.Values' and 'Bool'

标签: swiftfirebasefirebase-realtime-database

解决方案


我想dictionaryArray.values[Bool]这样,试试下面的代码也许它对你有用

var isFalse = false
for value in dictionaryArray.values {
  if value != true {
      isFalse = true
   }
}

if isFalse {
  print("All value is Not True")
} else {
  print("All value is True")
}

推荐阅读