嘿,我有一个使用typealias的代码,这样即使我的类型别名不起作用,当用户未正确注册时它也可以向用户发送错误代码,这给我一个问题,即我的课程突然当我使用类型别名时,会变成错误类型,有人可以告诉我为什么会这样。
我的代码
import Foundation
import UIKit
import FirebaseAuth
typealias Completion = (errMsg: String?, data: AnyObject?) -> Void
class AuthenticationDatabase{
var database = Database()
func register(user: [UITextField], onComplete: Completion?)-> String{
Auth.auth().createUser(withEmail: user[0].text, password:user[1].text) { (user, error) in
if(error != nil){
self.handleFirebaseError
}
}
}
func Login(UserInfo: [String]){
}
func logOut(){
}
func handleFirebaseError(error: NSError, onComplete:Completion?){
print(error.localizedDescription)
if let errorCode = AuthErrorCode(rawValue: error.code){
switch (errorCode){
case .errorCodeInvalidEmail:
onComplete?(errMsg: "Invalid Email", data: nil)
break
case .errorCodeWrongPassword:
onComplete?(errMsg: "invalid Password", data: nil)
break
default:
onComplete?(errMsg: "There was a problem Authenticating. Try again.",data: nil)
}
}
}
}
答案 0 :(得分:1)
将Completion
更改为
typealias Completion = (_ errMsg: String?, _ data: AnyObject?) -> Void
OR
typealias Completion = (String?, AnyObject?) -> Void
答案 1 :(得分:1)
在Swift 3+中,省略了闭包中的参数标签
typealias Completion = (String?, AnyObject?) -> Void
(强烈建议使用Any
而不是AnyObject
)
并调用它
onComplete?("Invalid Email", nil)