2015-09-09 88 views
-1

我只想打印兒童計數並將其設置爲標籤。uint不可轉換爲int

這是我的JSON結構:

  • simplelogin2

    • 年齡
      • POST1: 「asdasd」
      • p OST2: 「sadasd」
      • post3: 「asdasd」

我想打印兒童的數量在後期或將其設置爲一個標籤,我應該怎麼辦呢?

我嘗試這樣做:

ref.childByAppendingPath("users").childByAppendingPath(ref.authData.uid).childByAppendingPath("post").observeSingleEventOfType(.Value, withBlock: { snapshot in 

    self.postCount = snapshot.childrenCount.value 
    println(self.postCount) 

})' 

,但它說字是不可轉換爲uint。

UPDATE:

這是我創建的用戶:

var userId = authData.uid 

         let newUser = [ 
          "Provider" : authData.provider, 
          "email" : authData.providerData["email"] as? NSString as? String, 
          "name"  : self.Name.text, 
          "Image" : "", 
          "location" : "", 
          "about" : "", 
          "age"  : "", 
         ] 


self.ref.childByAppendingPath("users").childByAppendingPath(authData.uid).setValue(newUser) 

這是我添加的子 「後」 到每一個用戶!

(一個按鈕內我沒有這種編碼)

ref.childByAppendingPath("users/\(ref.authData.uid)/post").childByAutoId().setValue(text.text) 
+0

我是這樣做的bcoz我想打印登錄用戶的帖子的兒童數量! –

+0

問題不明確 – Mousavian

+0

發佈更多代碼或顯示錯誤 – iAnurag

回答

0

根據該火力地堡文檔,childrenCount是NSUInteger。

@property (readonly, nonatomic) NSUInteger childrenCount 

我會放棄.value調用,因爲它不需要。您還可以使用字符串插值將值轉換爲字符串。

ref.childByAppendingPath("users").childByAppendingPath(ref.authData.uid).childByAppendingPath("post").observeSingleEventOfType(.Value, withBlock: { snapshot in 

    self.postCount = snapshot.childrenCount 
    println("\(self.postCount)") 
}) 

如果你有一個UILabel的地方,你想設置postCount作爲文本,請執行以下操作。請注意,例如,我的UILabel被稱爲標籤。

label?.text = "\(self.postCount)" 

注意標籤上的問號。由於text是一個可選屬性,我們使用可選鏈來設置值。

+0

謝謝你的回覆,但是使用你的代碼,它在標籤 –

+0

上顯示nil,還有一件事情是當我在單一事件中輸入println(「\(self.postcount)」)方法,但在視圖內部確實加載它給我零,如果我在singleEvent方法中鍵入它正確打印計數..現在我該怎麼辦? @Mr Beardsley –

+0

謝謝你,先生!它工作:)我將標籤設置爲SingleEventMethod內的postCount ...再次感謝:) –