swift - 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;
解决方案
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()
.)
推荐阅读
- r - 如何在 R 中以闪亮的方式强制数据框中的反应
- python - TypeError:列表索引必须是整数或切片,而不是字符串
- c++ - 在这种情况下,为什么不是 constexpr ?
- neural-network - 当用户提出问题时,从 CSV 文件中的数据预测答案
- typescript - 作为对象的 Vue 道具的反应性
- java - GAE 任务推送失败
- r - 如何绘制只有足够数据的图?
- latex - 在 Gummi(LaTeX 编辑器)中设置预览窗口的默认缩放
- php - 如何比较for循环中的每个值?
- kubernetes - nginx 配置到 kubernetes ingress-nginx