首页 > 解决方案 > FirestoreSwift @DocumentID 无法在 Xcode 11 中编译

问题描述

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)")
      }
  }
}

我不希望将 a 传递[String: Any]init.

有任何想法吗?

谢谢

标签: swiftfirebasegoogle-cloud-firestore

解决方案


@DocumentID属性包装器确实有一个可选的init

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

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

    ...

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


推荐阅读