我正在尝试使用以下类型别名来传递函数以对其进行定义:
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;
答案 0 :(得分:1)
您可以这样写:
checkAuth(authFunc: Auth.auth().signIn(withEmail:password:completion:))
checkAuth(authFunc: Auth.auth().signIn(withEmail:link:completion:))
(您可能在#selector()
中发现了类似的符号。)