首页 > 解决方案 > Passing a function with the same name gets "ambiguous" error

问题描述

I am trying to pass a function using the following typealias to define it:

 typealias AuthFunction = (String, String, AuthDataResultCallback?) -> ()

this is then used in the following function:

    private func checkAuth(authFunc: AuthFunction) {
        if emailTextField.text == "" {
            showAlert(title: String.Localized.Common.error,
                      message: String.Localized.Common.enterEmailAndPassword)
        } else {
            guard let email = emailTextField.text,
                    let password = passwordTextField.text else { return }

            authFunc(email, password) { [weak self] (user, error) in
                guard let strongSelf = self else { return }
                strongSelf.checkAfterAuth(error)
            }
        }
    }

I have done this so I can call some Firebase auth functions that do the different things but have the same result. I also wanted to see if I could refactor this way as I've never tried it before.

It works fine when calling like so:

checkAuth(authFunc: Auth.auth().createUser)

The problem im running into is that the firebase SDK has a couple functions that begin with signIn:

signIn(withEmail: , password:, completion:)
signIn(with:, completion:)
signIn(withEmail:, link:, completion:)

This means when calling checkAuth(authFunc: Auth.auth().signIn) i get Ambiguous use of 'signIn' because signIn has multiple definitions.

Is there anyway around this?

Update:

Firebase definitions on both the calls:

- (void)createUserWithEmail:(NSString *)email
               password:(NSString *)password
             completion:(nullable FIRAuthDataResultCallback)completion;

- (void)signInWithEmail:(NSString *)email
           password:(NSString *)password
         completion:(nullable FIRAuthDataResultCallback)completion;

标签: swiftswift4

解决方案


You can write something like this:

checkAuth(authFunc: Auth.auth().signIn(withEmail:password:completion:))

checkAuth(authFunc: Auth.auth().signIn(withEmail:link:completion:))

(You may have found similar notations inside #selector().)


推荐阅读