FirestoreSwift @DocumentID在Xcode 11中无法编译

3
我正在使用FirebaseFirestoreSwift模块在Xcode 11项目中,但无法编译以下结构体。我收到了错误提示:

属性类型“String”与其包装类型“DocumentID”的“wrappedValue”属性不匹配

import FirebaseFirestoreSwift

struct CustomerLocation: Codable {
    @DocumentID var id: String  // <- error is here
    let contactEmail: String
    let contactName: String
    let contactPhoneNumber: String
    let serviceContactEmail: String
}

我正在进行一个简单的获取和文档解码,如下所示:

func fetchCustomerLocation(path: String) {
   firestore.document(path).getDocument{ (document, error) in
      guard error == nil, let document = document, document.exists else {
         print("Error retrieving customer location document. \(error!.localizedDescription)")
         return
      }

      do {
         let result = try document.data(as: CustomerLocation.self)
         print(result!)
      }
      catch let error {
         print("Error retrieving parsing location document. \(error.localizedDescription)")
      }
  }
}

我希望在init方法中不使用[String: Any]作为参数。

有什么好的想法吗?

谢谢


1
你尝试过使用可选项吗,例如 @DocumentID var id: String? - sllopis
1
你期望 @DocumentID 做什么?能否提供文档链接? - Doug Stevenson
我认为@DocumentID与Firebase产品无关。虽然有一个答案,但不清楚@DocumentID是什么 - 我们可以获得更多信息吗?因为互联网搜索没有显示任何相关内容。 - Jay
嗨,@Jay,我最终在Firebase iOS SDK的GitHub存储库中阅读问题,以了解“@DocumentID”。 https://github.com/firebase/firebase-ios-sdk/blob/master/Firestore/Swift/Source/Codable/DocumentID.swift - user9041624
2个回答

1
< p > @DocumentID 属性包装器确实具有可选的 init,如下所示:

public struct DocumentID<Value: DocumentIDWrappable & Codable & Equatable>:
    DocumentIDProtocol, Codable, Equatable {
    var value: Value?

    public init(wrappedValue value: Value?) {
      self.value = value
    }

    ...

所以来自sllopis的建议解决了我的问题。谢谢。


0
db.collection("entidades").addSnapshotListener { (querySnapshot, error) in
    guard let querySnapshot = querySnapshot else { return }
    self.entidades = querySnapshot.documents.compactMap { document -> Entidad? in 
        try? document.data(as: Entidad.self)
    }
}

3
请勿仅以代码作为答案,还需解释代码的功能以及如何解决问题。带有解释的答案通常质量更高,更容易得到赞同。 - Suraj Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接