Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios swift parse: How to handle error codes

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!

like image 530
user1555112 Avatar asked Feb 19 '15 12:02

user1555112


People also ask

How do you handle errors in Swift?

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.

What is nserror in Swift?

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.

How does the SDK handle errors in iOS Swift?

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.

What is the use of alert in Swift?

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.


2 Answers

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
        }
    }
}
like image 118
Christian Avatar answered Sep 30 '22 16:09

Christian


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

like image 26
Jeremy Piednoel Avatar answered Sep 30 '22 18:09

Jeremy Piednoel