ios swift 解析:如何处理错误代码

ios swift parse: How to handle error codes

在注册过程中,用户可能会导致一些错误,例如 用户名已被占用,电子邮件地址无效等...

在错误对象中解析 returns 所有需要的信息请参阅 http://parse.com/docs/dotnet/api/html/T_Parse_ParseException_ErrorCode.htm

我找不到的是如何使用它们,例如如何访问它们以编写一个开关来捕获所有可能性:

                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)
                    }
                }

如何切换可能的错误代码编号? 请指教 谢谢!

一个NSError也有一个属性叫做code。该代码包含您需要的错误代码。因此,您可以使用该代码创建一个 switch 语句:

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
        }
    }
}

你也可以使用PFErrorCode:

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 PFErrorCode.ErrorConnectionFailed.rawValue:
              println("ConnectionFailed")
           case PFErrorCode.ErrorObjectNotFound.rawValue:
              println("ObjectNotFound")
           default:
              break
        }
    }
}

嗨,我会做类似

if let error = error,let code = PFErrorCode(rawValue: error._code) {
    switch code {
    case .errorConnectionFailed:
        print("errorConnectionFailed")
    case .errorObjectNotFound:
        print("errorObjectNotFound")
    default:
        break
    }
}

你有完整的错误列表:https://github.com/parse-community/Parse-SDK-iOS-OSX/blob/master/Parse/PFConstants.h#L128