无法使用完成块推断静态函数上的泛型类型

朋朋

我有一个使用泛型的静态函数,但无法在调用泛型时推断出它。功能:

static func getDocument<T: JSONDecodable>(_ document: String, fromCollection collection: FirebaseStorage.FirestoreCollections, completion: @escaping (_ decodedDoc: T?, _ error: Error?) -> ()) {
    let docRef = firestore.collection(collection.rawValue).document(document)
    docRef.getDocument { documentSnapshot, error in
        guard error == nil,
            let docData = documentSnapshot?.data(),
            let decodedDoc = T(json: docData) else {
            completion(nil, error)
            return
        }
        completion(decodedDoc, nil)
    }
}

调用使用:

FirebaseClient.getDocument(
    id,
    fromCollection: FirebaseStorage.FirestoreCollections.users) { (profile, error) in

}

出现错误:Generic parameter 'T' could not be inferred如何使函数的通用部分起作用?

凯恩·柴郡
FirebaseClient.getDocument(
    id,
    fromCollection: FirebaseStorage.FirestoreCollections.users) { (profile: ProfileType?, error) in

}

您需要让Swift知道profile我添加的类型什么ProfileType那应该做!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章