Swiftui 条件绑定的初始值设定项必须具有可选类型,而不是';T.ID';

Swiftui 条件绑定的初始值设定项必须具有可选类型,而不是';T.ID';,swiftui,swift5,Swiftui,Swift5,我有以下带有Firebase函数的类: class FIRFirestoreService { private init() {} static let shared = FIRFirestoreService() func configure() { FirebaseApp.configure() } private func reference(to collectionReference: FIRCollectionReference) -> CollectionRef

我有以下带有Firebase函数的类:

class FIRFirestoreService {

private init() {}
static let shared = FIRFirestoreService()

func configure() {
    FirebaseApp.configure()
}

private func reference(to collectionReference: FIRCollectionReference) -> CollectionReference {
    return Firestore.firestore().collection(collectionReference.rawValue)
}

func create<T: Encodable>(for encodableObject: T,in collectionReference: FIRCollectionReference) {
    do {
        let json = try encodableObject.toJson(excluding: ["id"])
        reference(to: collectionReference).addDocument(data: json)
    } catch {
        print(error)
    }
}

func read<T: Decodable>(from collectionReference: FIRCollectionReference, returning objectType: T.Type, completion: @escaping ([T]) -> Void) {
    reference(to: collectionReference).addSnapshotListener { (snapshot, _) in
        guard let snapshot = snapshot else { return }
        do {
            var objects = [T]()
            for document in snapshot.documents {
                let object = try document.decode(as: objectType.self)
                objects.append(object)
            }
            completion(objects)
        } catch {
            print(error)
        }
    }
}

func update<T: Encodable & Identifiable>(for encodableObject: T, in collectionReference: FIRCollectionReference) {
    do {
        let json = try encodableObject.toJson(excluding: ["id"])
        guard let id = encodableObject.id else { throw MyError.encodingError }
        reference(to: collectionReference).document(id).setData(json)
    } catch {
        print(error)
    }
}

func delete<T: Identifiable>(_ identifiableObject: T, in collectionReference: FIRCollectionReference) {
    do {
        guard let id = identifiableObject.id else { throw MyError.encodingError }
        reference(to: collectionReference).document(id).delete()
    } catch {
        print(error)
    }
}
}

这是快照+扩展名文件

extension Encodable {
func toJson(excluding keys: [String] = [String]()) throws -> [String : Any] {
    let objectData = try JSONEncoder().encode(self)
    let jsonObject = try JSONSerialization.jsonObject(with: objectData, options: [])
    guard var json = jsonObject as? [String : Any] else { throw MyError.encodingError }

    for key in keys {
        json[key] = nil
    }

    return json
}
extension DocumentSnapshot {
func decode<T: Decodable>(as objectType: T.Type, includingId: Bool = true) throws -> T {
    var documentJson = data()
    if includingId {
        documentJson!["id"] = documentID
    }

    let documentData = try JSONSerialization.data(withJSONObject: documentJson, options: [])
    let decodedObject = try JSONDecoder().decode(objectType, from: documentData)

    return decodedObject
}
使用以下内容标记:

条件绑定的初始值设定项必须具有可选类型,而不是“T.ID”


这是为什么?我该如何安装它?

错误很明显,它表明
保护
对象是非可选的

实际上,需要一个非可选的
id
属性,因此需要替换

guard let id = identifiableObject.id else { throw MyError.encodingError }


请显示您使用它的代码,它看起来像是泛型解析期间的类型冲突。这就是问题所在,我还没有能够使用它。我将在下面发布所有相关代码…我已经用完整的代码编辑了初始问题
guard let id = identifiableObject.id else { throw MyError.encodingError }
let id = identifiableObject.id