【问题标题】:Cannot convert value of type '() -> ()' to expected argument type '((AuthDataResult?, Error?) -> Void)?'无法将类型“()->()”的值转换为预期的参数类型“((AuthDataResult?,错误?)-> Void)?”
【发布时间】:2020-09-29 13:55:57
【问题描述】:
static func signUp(username: String, email: String, password: String, imageData: Data, onSuccess: @escaping () -> Void, onError:  @escaping (_ errorMessage: String?) -> Void) {
        Auth().createUser(withEmail: email, password: password, completion: {            if error != nil {
                onError(error!.localizedDescription)
                return
            }
            let uid = user?.uid
            let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOF_REF).child("profile_image").child(uid!)
            
            storageRef.put(imageData, metadata: nil, completion: { (metadata, error) in
                if error != nil {
                    return
                }

【问题讨论】:

  • 您有问题吗?您刚刚发布了一段代码,没有任何解释。我猜这是 Firebase,但你也应该提到

标签: swift xcode


【解决方案1】:

没有使用onSuccess 作为完成,所以你可以删除它,我从你的代码中看到的是,一旦它发生,你正试图将Error 作为String 接收。所以,我已经相应地更新了该代码。

 static func signUp(username: String, email: String, password: String, imageData: Data, onError:  @escaping (_ errorMessage: String?) -> Void) {

      Auth().createUser(withEmail: email, password: password, completion: {         
              if let error = error {
                    onError(error.localizedDescription)
                    return
                }

            let uid = user?.uid
            let storageRef = Storage.storage().reference(forURL: Config.STORAGE_ROOF_REF).child("profile_image").child(uid!)
            
            storageRef.put(imageData, metadata: nil, completion: { (metadata, error) in
                if let error = error {
                   onError(error.localizedDescription)
                   return
                }
                   onError(nil)
                })

【讨论】:

    【解决方案2】:

    Firebase Auth().createUser 中的完成块的签名为 ((AuthDataResult?, Error?)->Void)?即它需要两个参数:结果和错误。您没有包含任何内容,因此编译器假定您的完成块签名为 ()->()

    调用的第一行应该是:

    Auth().createUser(withEmail: email, password: password, completion: { result, error in 
    

    参见例如https://stackoverflow.com/a/36586581/1852207

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-05
      • 2017-01-13
      • 2023-04-04
      • 2020-03-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-18
      • 1970-01-01
      相关资源
      最近更新 更多