我正在创建一个函数,用于在其中验证密码,我尝试对其进行调试并寻找可能的解决方案,但似乎我无法为其寻找更好的解决方案。
func validateEventPasscode(){
//Show Loading
self.view.squareLoading.start(0.0)
let api = APIService ()
api.validatePasscode(eventcode: eventCode) { (data, error) in
guard let eventDetails = self.event, error == nil else {
if let networkError = Error.self {
if networkError == .InvalidCredentials {
_ = SCLAlertView(appearance: appearance).showError("Ooops!", subTitle: "Please enter a valid event passcode")
}else {
_ = SCLAlertView(appearance: appearance).showError("Network Error", subTitle: "\(networkError.rawValue)")
}
}
self.view.squareLoading.stop(0.0)
return
}
guard eventDetails.deleteFlag == false else {
_ = SCLAlertView(appearance: appearance).showError("Ooops!", subTitle: "Please enter a valid event passcode")
self.view.squareLoading.stop(0.0)
return
}
if (eventDetails.closedFlag == true && eventDetails.reopenFlag == false) {
_ = SCLAlertView(appearance: appearance).showError("Closed Event", subTitle: "Please check the status of your event and try again")
self.view.squareLoading.stop(0.0)
return
}
.InvalidCredentials来自何处
enum NetworkError: Error {
case InvalidCredentials
case custom(String)
case other
}
extension NetworkError: LocalizedError {
var errorDescription: String? {
switch self {
case .InvalidCredentials:
return "Invalid event code"
case .other:
return "Something went wrong"
case .custom:(let message):
return message
}
}
}
我将此链接用作我在 validatePasscode validatePasscode reference
中使用的APIService的参考答案 0 :(得分:1)
似乎您正在尝试将error
强制转换为Error
类型的对象,可以这样做
if let networkError = error as? Error {
if networkError == .InvalidCredentials {
_ = SCLAlertView(appearance: appearance).showError("Ooops!", subTitle: "Please enter a valid event passcode")
}else {
_ = SCLAlertView(appearance: appearance).showError("Network Error", subTitle: "\(networkError.rawValue)")
}
}
编辑-这是您的固定代码
enum NetworkError: Error {
case invalidCredentials
case custom(String)
case other
}
extension NetworkError: LocalizedError {
var errorDescription: String? {
switch self {
case .invalidCredentials:
return "Invalid event code"
case .other:
return "Something went wrong"
case .custom(let message):
return message
}
}
}
func validateEventPasscode() {
//Show Loading
self.view.squareLoading.start(0.0)
let api = APIService()
api.validatePasscode(eventcode: eventCode) { (data, error) in
guard let eventDetails = self.event, error == nil else {
guard let error = error as? NetworkError else { return }
switch error {
case .invalidCredentials:
_ = SCLAlertView(appearance: appearance).showError("Ooops!", subTitle: "Please enter a valid event passcode")
case .custom(let message):
print(message)
case .other:
_ = SCLAlertView(appearance: appearance).showError("Network Error", subTitle: "\(networkError.rawValue)")
}
self.view.squareLoading.stop(0.0)
return
}
guard !eventDetails.deleteFlag else {
_ = SCLAlertView(appearance: appearance).showError("Ooops!", subTitle: "Please enter a valid event passcode")
self.view.squareLoading.stop(0.0)
return
}
if eventDetails.closedFlag, !eventDetails.reopenFlag {
_ = SCLAlertView(appearance: appearance).showError("Closed Event", subTitle: "Please check the status of your event and try again")
self.view.squareLoading.stop(0.0)
return
}
}
}
答案 1 :(得分:0)
您必须可选地绑定可选的error
参数,而不是非可选的Error
类型。
我建议使用这种语法(eventDetails
似乎没用)
func validateEventPasscode(){
//Show Loading
self.view.squareLoading.start(0.0)
let api = APIService ()
api.validatePasscode(eventcode: eventCode) { (data, error) in
if let networkError = error {
if networkError == .InvalidCredentials {
_ = SCLAlertView(appearance: appearance).showError("Ooops!", subTitle: "Please enter a valid event passcode")
} else {
_ = SCLAlertView(appearance: appearance).showError("Network Error", subTitle: "\(networkError.rawValue)")
}
} else {
self.view.squareLoading.stop(0.0)
}
}
}