无法将类型(User ?, Error?)->()'的值转换为预期的参数类型'AuthDataResultCallback?

时间:2019-03-05 15:55:02

标签: ios swift xcode firebase authentication

我知道这个问题已经在这里得到解答:Cannot convert value of type '(User?, Error?) -> ()' to expected argument type 'AuthDataResultCallback?',但不能解决我的问题。我在用于创建用户的两个独立项目中具有相同的代码(下)。一种是没有错误返回,而另一种是上面的错误返回。我完全不知道为什么,并希望有些人知道该怎么做。我认为这可能与依赖关系有关,但是我的podfile已更新,并且我在终端中也做了同样的事情。有什么帮助吗?我的代码:

import Foundation
import FirebaseAuth
import FirebaseStorage
import FirebaseDatabase

class AuthService {

static func logIn(email: String, password: String, onSuccess: @escaping () -> Void, onError: @escaping (_ errorMessage: String?) -> Void) {
    Auth.auth().signIn(withEmail: email, password: password, completion: { (user, error) in
        if error != nil {
            onError(error!.localizedDescription)
            return
        }
        onSuccess()
    })
}

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

    func checkUserNameAlreadyExist(newUserName: String, completion: @escaping(Bool) -> Void) {

        let ref = Database.database().reference()
        ref.child("users").queryOrdered(byChild: "username").queryEqual(toValue: newUserName)
            .observeSingleEvent(of: .value, with: {(snapshot: DataSnapshot) in

                if snapshot.exists() {
                    completion(true)
                }
                else {
                    completion(false)
                }
            })
    }

    checkUserNameAlreadyExist(newUserName: username) { isExist in
        if isExist {
            onError("This username is taken")
            return
        }
        else {
            Auth.auth().createUser(withEmail: email, password: password, completion: { (user: User?, error: Error?) in
                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.putData(imageData, metadata: nil, completion: { (metadata, error) in
                    if error != nil {
                        return
                    }

                    let profileImageUrl = metadata?.downloadURL()?.absoluteString
                    self.setUserInformation(profileImageUrl: profileImageUrl!, username: username, email: email, uid: uid!, onSuccess: onSuccess)
                })
            })
        }
    }
}

static func setUserInformation(profileImageUrl: String, username: String, email: String, uid: String, onSuccess: @escaping () -> Void) {

    let ref = Database.database().reference()
    let usersReference = ref.child("users")
    let newUserReference = usersReference.child(uid)
    newUserReference.setValue(["username": username, "lowercaseUsername": username.lowercased(), "email": email, "profileImageUrl": profileImageUrl, "friends": 0])
    onSuccess()
}
}

特别是:

Auth.auth().createUser(withEmail: email, password: password, completion: { (user: User?, error: Error?) in
    ...
})

0 个答案:

没有答案