Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 从';[[AnyHashable:Any]]';不相关类型';[AnyHashable:Any]';总是失败 更新_Ios_Swift_Firebase - Fatal编程技术网

Ios 从';[[AnyHashable:Any]]';不相关类型';[AnyHashable:Any]';总是失败 更新

Ios 从';[[AnyHashable:Any]]';不相关类型';[AnyHashable:Any]';总是失败 更新,ios,swift,firebase,Ios,Swift,Firebase,我怎样才能解决这个问题 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 =

我怎样才能解决这个问题

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:Any]]”转换为不相关的类型“[AnyHashable:Any]”总是失败

考虑到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
let updateUser=[“name”:“Shannon”,“username”:“Shannon规则”]
let ref=Firebase(url:https://.firebaseio.com")
让fanoutObject=[“/users/1”:updateuser“/usersWhoAreCool/1”:updateuser“/userstogivefreestufto/1”,updateuser]
ref.updateChildValues(updateUser)//原子更新

,并希望将其实施到我的项目中。但问题是我不太明白它在说什么

有人能简单地解释扇出功能是如何工作的吗?

例如:

这里发生了什么

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
let updateUser=[“name”:“Shannon”,“username”:“Shannon规则”]
let ref=Firebase(url:https://.firebaseio.com")
让fanoutObject=[“/users/1”:updateuser“/usersWhoAreCool/1”:updateuser“/userstogivefreestufto/1”,updateuser]
ref.updateChildValues(updateUser)//原子更新

似乎从未使用过fanoutObject

您已经创建了四个
[AnyHashable:Any]
字典。要将这些词典作为另一个
[AnyHashable:Any]
词典加入,您不应该将词典包装在数组中,并键入大小写作为
[AnyHashable:Any]
。使用reduce和merging方法联接所有字典

let dataToUpdate = [imageURL,postTimeValue,timeValue,caption].reduce([AnyHashable: Any]()) { (result, dict) in
    result.merging(dict, uniquingKeysWith: { (_, new) in new })
}
ref.updateChildValues(dataToUpdate)


看起来最后一行应该使用
fanoutObject
而不是
updateuser
。你试过了吗?试过了,看起来是个错误。整篇文章中的swift语法也有很多错误@FrankvanPuffelenI如果你给我一个文章的链接,我会看看我们是否可以更新它。还更新了我得到的代码顶层第一行的问题:表达式的类型是不明确的,没有更多信息context@NCT127你能添加一个屏幕截图吗?在这一行:让dataToUpdate=[imageURL,postTimeValue,timeValue,caption].reduce([AnyHashable:Any]()){(result,dict)in@NCT127试试最后一个街区
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 }