uint 不可转换为 int

uint not convertible to int

我只想打印 children 计数并将其设置为标签。

这是我的 JSON 结构:

我想在post中打印children的个数,或者设置到标签中,我应该怎么做?

我试过这个:

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

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

})'

但它说 word 不能转换为 uint。

更新:

这就是我创建用户的方式:

  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)

这就是我向每个用户添加 child "post" 的方式!

(在按钮内我做了这个编码)

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

根据 Firebase 文档,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)")
})

如果您想将 postCount 设置为文本的某处有 UILabel,请执行以下操作。请注意,例如,我的 UILabel 仅称为标签。

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

注意标签上的问号。由于文本是可选的 属性,我们使用可选链接来设置值。