我正在尝试使用以下类型别名来定义它来传递一个函数:
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/