swift - 传递具有相同名称的函数会出现 "ambiguous"错误

标签 swift swift4

我正在尝试使用以下类型别名来定义它来传递一个函数:

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

然后在以下函数中使用它:

    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)
            }
        }
    }

我这样做是为了调用一些 Firebase 身份验证函数,它们执行不同的操作但结果相同。我还想看看我是否可以用这种方式重构,因为我以前从未尝试过。

像这样调用时工作正常:

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

我遇到的问题是 firebase SDK 有几个以 signIn 开头的函数:

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

这意味着当调用 checkAuth(authFunc: Auth.auth().signIn) 时,我得到了 Ambiguous use of 'signIn' 因为 signIn有多个定义。

有什么办法吗?

更新:

两个调用的 Firebase 定义:

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

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

最佳答案

你可以这样写:

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

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

(您可能在 #selector() 中发现了类似的符号。)

关于swift - 传递具有相同名称的函数会出现 "ambiguous"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51694983/

相关文章:

swift - 如何在捕获中打印错误

swift - 反射(reflection):在Swift中获取类的静态var?

ios - Swift 结构体返回成员

swift - 在 Swift 中听标准输入

iOS 11 : Add other nav items next to a search bar embedded in navigation item title view

swift - quicklook总是显示 "no file to preview"错误(url有效)

swift - 快速获取触摸的各种坐标

ios - 由于列表,导航 View 的背景颜色未显示

ios - 无法访问 kABPersonNoteProperty

IOS WKWebView 文件上传不工作