我正在检查数据库,以查看是否存在诸如Plumber: true
或Electrician: true
的键值对。
如果根本不存在,我想创建一个新的键值对:
(例如)Plumber: true
虽然如果它确实存在并且为真,我想将其设置为false;如果它确实存在,并且如果为false,我想将其设置为true
我的代码在did select row
函数中
变量“ serviceType”来自行的单元格处的label.text
我从firebase中的当前用户数据返回字典
let dictionary = document.data()
let serviceType = dictionary[String(describing: service)]
if serviceType == nil {
// if field doesn't exist create a new field and set to true
db.collection("Users").document(uid).setData([String(describing: service): true ], options: SetOptions.merge())
//else if it does exist check if true or false and set to the opposite
} else if serviceType == true {
db.collection("Users").document(uid).setData([String(describing: service): false ], options: SetOptions.merge())
} else if serviceType == false {
//set to true
}
在这一行:
} else if serviceType == true {
我收到错误
二进制运算符'=='不能应用于'Any?'类型的操作数和“布尔”
最佳答案
将您的serviceType变量强制转换为Bool:
let serviceType = dictionary[String(describing: service)] as? Bool