在 Swift 中解析火力库时为 nil



我正在尝试将值从火碱解析到我的字典:

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"])
                    }
            }

这是我的字典声明:

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

restDict 值返回我一个 Optional("somestring"(,但 elf.users["userName"] 返回 nil。我怎样才能在我的字典中获得真正的价值?提前谢谢。

在下面的行中,您尝试为键restDict["userName"]!设置值restDict["userImgLink"]!,因此键将是restDict["userName"]!的值而不是"userName"

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

但是在打印时,您正在尝试获取"userName"键的值,该值将为 nil。

最新更新