2017-04-05 50 views
0

解析火力點我試圖解析從火力點值到我的字典:零而迅速

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值返回我的可選(」 somestring「),但elf.users [」userName「]返回nil。我怎麼能在我的字典中獲得真正的價值?提前致謝。

+0

我會建議做一些調試,並檢查'​​restDict'中的所有值的值是什麼,這將是很好的添加一個作爲代碼片段... –

+0

你確定關鍵的userName存在嗎? – Anuraj

+0

@Anuraj是的,它返回我可選(「名稱」) – Burning

回答

0

在下面的行中,您試圖爲密鑰restDict["userName"]!設置值restDict["userImgLink"]!,因此密鑰將是值restDict["userName"]!而不是"userName"

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

但在打印時,您試圖獲取"userName"鍵的值,該值將爲零。

+0

謝謝,我應該寫在這種情況下?如果我刪除「!」,我無法構建我的應用程序。 – Burning