【问题标题】:Update string with firebase swift使用 firebase swift 更新字符串
【发布时间】:2019-06-12 17:43:05
【问题描述】:

我正在尝试使用 firebase swift 更新字符串,但我遇到了一个我不知道如何摆脱的错误。

我的代码部分出现错误:

self.dbRef.child("feed-items/\(dataPathen)/likesForPost").updateChildValues("likesForPost": "7") 

我得到的错误是expected "," seperator 就在: 之前。我在另一个代码部分中使用 dbRef,所以我知道我可以工作,并且 dataPathen 正在上面的代码部分之前打印,所以它也可以工作。

谁能帮我解决这个错误?

【问题讨论】:

    标签: swift string firebase firebase-realtime-database


    【解决方案1】:

    改变一下

    self.dbRef.child("feed-items/\(dataPathen)/likesForPost").updateChildValues("likesForPost": "7") 
    

    收件人

    self.dbRef.child("feed-items/\(dataPathen)/likesForPost").updateChildValues(["likesForPost": "7"]) 
    

    如果您只想在特定节点上增加特定值,您可能想查看我的答案:- https://stackoverflow.com/a/39465788/6297658, https://stackoverflow.com/a/39471374/6297658

    PS 更喜欢 runTransactionBlock: 来更新 likeForPosts 这样的属性,因为可能会有两个用户同时点赞同一个帖子(极不可能,但仍有可能...),使用updateChildValues 可能最终只会像仅从一个用户一样更新。但是 runTransactionBlock: 一直触发,直到该线程的更改已提交到节点

    【讨论】:

    • 我更新了你的建议,但是当我尝试使用它说的代码时:fatal error: unexpectedly found nil while unwrapping an Optional value
    • 我正在使用.updateChildValues,但我会尝试另一种我只需要弄清楚如何使其适合我的代码。
    • 有什么地方可以聊天吗?我有一些事情希望你能帮助我,因为我无法让我的喜欢按钮的功能以我想要的方式工作
    【解决方案2】:

    updateChildValues 接受[AnyHashable:Any] 字典:

    self.dbRef.child("feed-items/\(dataPathen)/likesForPost")
        .updateChildValues(["likesForPost": "7"])
    

    【讨论】:

    • 我试过你的代码,但它给了我这个错误:fatal error: unexpectedly found nil while unwrapping an Optional value...我也尝试删除子参考中的 likesForPost 但没有运气
    • @D.Finna 可能是因为dataPathenOptional
    • 您能否用完整的示例更新您的问题,包括 dbRef 和 dataPathen 声明?
    • 感谢您对 mixel 的帮助,但我想通了使用 Dravidian 的代码。
    【解决方案3】:
    1. 每当更新 Firebase 数据库中任何引用的值时,您需要将 updateChildValues 方法的字典参数作为 [AnyHashable: Any] 传递给您的路径引用。所以只需更新您的代码行如下:
    2. self.dbRef.child("feed-items/(dataPathen)/likesForPost").updateChildValues("likesForPost": "7")
    3. 此外,如果您需要更新超过 1 个键值对,则可以通过使用逗号分隔将这些键值对传递到字典中,如下所示:
    4. self.dbRef.child("feed-items/(dataPathen)/likesForPost").updateChildValues(["likesForPost": "7", "otherKey": "OtherKeyValue"])

    【讨论】:

      猜你喜欢
      • 2016-11-05
      • 2015-12-08
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-18
      相关资源
      最近更新 更多