【问题标题】:How to push data with multiple types in Firebase?如何在 Firebase 中推送多种类型的数据?
【发布时间】:2015-02-16 00:44:49
【问题描述】:

我想推送一个包含字符串、数字和日期的数组。我必须单独更新还是有其他方法可以做到这一点?

例子:

var categories : [String] = self.parseCategories()

var standbyDataStrings = [
    "firstName":firstName,
    "lastName":lastName,
    "categories": categories,
    "time_stamp":self.date.timeIntervalSince1970
]
var standbyDataNums = [
    "radius":nf.numberFromString(self.helpRadiusLabel.text!),
    "duration":nf.numberFromString(self.helpDurationLabel.text!)        
]
standbyUserRef.updateChildValues(standbyDataStrings)
standbyUserRef.updateChildValues(standbyDataNums)  // this gives me a error "string is not identical to NSObject"

结合standByDataStrings 和standbyDataNums 给我一个错误。

或者有没有办法从 Firebase 检索字符串并将其用作 int。它被存储为带有引号的字符串。

【问题讨论】:

    标签: ios swift firebase


    【解决方案1】:

    Firebase API 要求 NSDictionary 对应 updateChildValuesNSDictionary 不能包含 nil 值。另一方面,普通的 Swift 字典可以包含 nil 值。

    numberFromString 的返回类型是NSNumber?,所以 Swift 推断字典可能包含nil,因此它不能传递给updateChildValues。通过使用! 显式强制非零值,您可以编译此代码:

    var standbyDataNums = [ 
        "radius":nf.numberFromString(self.helpRadiusLabel.text!)!,
        "duration":nf.numberFromString(self.helpDurationLabel.text!)!        
    ]
    standbyUserRef.updateChildValues(standbyDataNums) // now type checks
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-07
      • 1970-01-01
      • 2013-09-08
      • 2018-04-08
      • 2015-05-29
      • 1970-01-01
      相关资源
      最近更新 更多