将可选数据写入Firebase

时间:2016-11-01 17:11:45

标签: ios firebase firebase-realtime-database swift3

使用Firebase Swift 2版本,我能够做到:

something = ["someOptionalData": optionalData]
ref.updateChildValues(something,......})

转换为Swift 3&最新的Firebase,我收到了这个错误:

Cannot store object of type _SwiftValue at optionalData. Can only store objects of type NSNumber, NSString, NSDictionary, and NSArray.

经过一些谷歌搜索,人们都说firebase不接受可选类型。

所以我可以知道除此之外处理它的更好方法是什么:

if let unwrappedOptionalData = optionalData {
        something = [
            "someOptionalData": unwrappedOptionalData
        ]
} else {
        something = [
            "someOptionalData": true
        ]
}
ref.updateChildValues(something,......)

1 个答案:

答案 0 :(得分:0)

尝试: -

something = ["someOptionalData": optionalData!]
ref.updateChildValues(something,......)

鉴于 optionalData 的类型为: - NSNumber NSString NSDictionary NSArray