简体   繁体   English

快速解析firebase时为零

[英]nil while parsing firebase in swift

I am trying to parse value from firebase to my dictionary: 我正在尝试将Firebase的值解析为我的字典:

for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {

                guard let restDict = rest.value as? [String: String] else {
                    continue
                }
                if (restDict["uid"] != FIRAuth.auth()?.currentUser?.uid && (restDict["userCity"])! == "SomeCity"){

                    self.users.updateValue(restDict["userImgLink"]!, forKey: restDict["userName"]!)
                        print(self.users["userName"])

                    }
            }

And here is my dict declaration: 这是我的字典声明:

var users: [String : String] = [:]

restDict value returns me an Optional("somestring"), but elf.users["userName"] returns nil. restDict值返回一个Optional(“ somestring”),但是elf.users [“ userName”]返回nil。 How could I get a real value in my dict? 我怎样才能在我的词典中获得真正的价值? Thanks in advance. 提前致谢。

In the below line you are trying to set the value restDict["userImgLink"]! 在下面的行中,您尝试设置值restDict["userImgLink"]! for key restDict["userName"]! 为键restDict["userName"]! , so the key will be the value of restDict["userName"]! ,因此键将为restDict["userName"]! and not "userName" . 而不是"userName"

 self.users.updateValue(restDict["userImgLink"]!, forKey: restDict["userName"]!)

but while printing you are trying to get the value for "userName" key , which will be nil. 但是在打印时,您试图获取"userName"键的值,该值为nil。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM