【问题标题】:Why is FieldValue.increment() giving me an error in Swift?为什么 FieldValue.increment() 在 Swift 中给我一个错误?
【发布时间】:2019-09-25 04:39:17
【问题描述】:

所以我唯一改变的是添加:

pod 'Geofirestore'

到我使用 Firestore 的 IOS 应用程序的 podfile,其中包含以下内容:

  pod 'Firebase/Core'
  pod 'Firebase/Firestore'
  pod 'Firebase/Auth'
  pod 'Firebase/Storage'
  pod 'GooglePlaces'
  pod 'GoogleMaps'
  pod 'Geofirestore'
  pod 'InstantSearch', '~> 5.0'

突然间,我在 Firestore 中更新字段值的代码行抛出了错误。行:

db.collection("people").document(userID!).updateData(["listsCount": FieldValue.increment(Int64(1))])

现在给我错误:

类型'FieldValue'没有成员'increment'

对于我的一生,我无法弄清楚突然发生了什么变化。任何帮助表示赞赏。

【问题讨论】:

  • FieldValue 是否也在 Geofirestore 中定义?尝试使用 Firebase.FieldValue.increment。
  • 也许Geofirestore 也有FieldValue 类型,编译器会选择那个类型而不是你想要的类型。尝试使用完全限定名称:ModuleName.TypeIWantToUse(例如,Firebase.FieldValue,如果讨论中的类型是 Firebase 的一部分。
  • 我不相信 Geofirestore 有类似 FieldValue 的东西,所以它不太可能被另一个库践踏。此外,Swift 非常聪明,知道调用是什么。我建议清理构建文件夹并重新构建您的项目。 (我刚刚将 Geofirestore 添加到一个使用 FieldValue 的项目中,它工作正常。)
  • 遇到同样的问题!
  • 也许this thread 可以提供帮助。

标签: ios swift firebase google-cloud-firestore geofirestore


【解决方案1】:

我能够通过在更新字段之前将我的值初始化为 Double 来解决此问题。这样,Firebase 就知道如何解释增量,可以是 DoubleInt64

   let value: Double = 1

    ref.updateData([
        "likeCount" : FieldValue.increment(value)

    ])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-28
    • 2018-12-23
    • 2017-12-17
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多