While on a signup process, the user can cause several errors like Username already taken, invalid email address etc...
Parse returns within the error object all needed infos see http://parse.com/docs/dotnet/api/html/T_Parse_ParseException_ErrorCode.htm
What I can't find out is how to use them eg how to access them in order to write a switch to catch all possibilities:
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if error == nil {
// Hooray! Let them use the app now.
self.updateLabel("Erfolgreich registriert")
} else {
println(error.userInfo)
}
}
What can I do to switch through the possible error code numbers? Please advice Thanks!
There are four ways to handle errors in Swift. You can propagate the error from a function to the code that calls that function, handle the error using a do - catch statement, handle the error as an optional value, or assert that the error will not occur. Each approach is described in a section below.
Error handling in Swift interoperates with error handling patterns that use the NSError class in Cocoa and Objective-C. For more information about this class, see Handling Cocoa Errors in Swift. In Swift, errors are represented by values of types that conform to the Error protocol.
The SDK handles potential errors and provide you with appropriate information so that you can process errors appropriately in your final product. All of the methods in the LINE SDK for iOS Swift returns an Result enumeration as its response.
In this example, an Alert will be used to show the errors – actual apps might come up with a more sophisticated UI. I recommend using the ↗ Error protocol to represent all error situations in Swift code.
An NSError
also has a property called code
. That code contains the error-code you need. So you can make a switch-statement with that code:
user.signUpInBackgroundWithBlock {
(succeeded: Bool!, error: NSError!) -> Void in
if error == nil {
// Hooray! Let them use the app now.
self.updateLabel("Erfolgreich registriert")
} else {
println(error.userInfo)
var errorCode = error.code
switch errorCode {
case 100:
println("ConnectionFailed")
break
case 101:
println("ObjectNotFound")
break
default:
break
}
}
}
Hi i would do something like
if let error = error,let code = PFErrorCode(rawValue: error._code) {
switch code {
case .errorConnectionFailed:
print("errorConnectionFailed")
case .errorObjectNotFound:
print("errorObjectNotFound")
default:
break
}
}
You have the whole errors list there : https://github.com/parse-community/Parse-SDK-iOS-OSX/blob/master/Parse/PFConstants.h#L128
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With