我该如何解决?
guard let imgUrl = url?.absoluteString else { return }
let imageURL = [AnyHashable("imageUrl"): imgUrl] as [AnyHashable : Any]
let postTimeValue = [AnyHashable("postTimeStamp"): currentPostTimeStamp] as [AnyHashable: Any]
let timeValue = [AnyHashable("timeStamp"): media.timeStamp!] as [AnyHashable: Any]
let mediaRef = postRef.child("\(mediaNum)")
let caption = [AnyHashable("Caption") : caption] as [AnyHashable: Any]
let dataToUpdate = [imageURL, caption, timeValue, postTimeValue] as? [AnyHashable: Any]
mediaRef.updateChildValues(dataToUpdate!)
我在最后一行收到波纹管错误
从“ [[[AnyHashable:任何]]”到不相关类型“ [AnyHashable:任何]”的投射始终失败
在Firebase给出的这个示例中,我认为它会起作用:
let updatedUser = ["name": "Shannon", "username": "shannonrules"]
let ref = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com")
let fanoutObject = ["/users/1": updatedUser, "/usersWhoAreCool/1": updatedUser, "/usersToGiveFreeStuffTo/1", updatedUser]
ref.updateChildValues(updatedUser) // atomic updating goodness
I have read this article from firebase,并希望将其实施到我的项目中。但是,问题是我不太理解它在说什么。
有人可以简单地解释扇出功能的工作原理吗?
例如:
这是怎么回事?
let updatedUser = ["name": "Shannon", "username": "shannonrules"]
let ref = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com")
let fanoutObject = ["/users/1": updatedUser, "/usersWhoAreCool/1": updatedUser, "/usersToGiveFreeStuffTo/1", updatedUser]
ref.updateChildValues(updatedUser) // atomic updating goodness
似乎从未使用过fanoutObject。
答案 0 :(得分:1)
您已经创建了四个[AnyHashable : Any]
词典。要将这些字典作为另一个[AnyHashable : Any]
字典加入,您不应该将字典包装在数组中并将case键入为[AnyHashable : Any]
。使用减少和合并方法来加入所有字典
let dataToUpdate = [imageURL,postTimeValue,timeValue,caption].reduce([AnyHashable: Any]()) { (result, dict) in
result.merging(dict, uniquingKeysWith: { (_, new) in new })
}
ref.updateChildValues(dataToUpdate)
或
var dataToUpdate = [AnyHashable: Any]()
guard let imgUrl = url?.absoluteString else { return }
dataToUpdate[AnyHashable("imageUrl") = imgUrl as Any
dataToUpdate[AnyHashable("postTimeStamp") = currentPostTimeStamp as Any
dataToUpdate[AnyHashable("timeStamp") = media.timeStamp! as Any
let mediaRef = postRef.child("\(mediaNum)")
dataToUpdate[AnyHashable("Caption") = caption as Any
ref.updateChildValues(dataToUpdate)
或
var dataToUPdate = [AnyHashable: Any]()
dataToUPdate.merge(imageURL) { (_, new) in new }
dataToUPdate.merge(postTimeValue) { (_, new) in new }
dataToUPdate.merge(timeValue) { (_, new) in new }
dataToUPdate.merge(caption) { (_, new) in new }