【问题标题】:ios swift parse: How to handle error codesios swift parse:如何处理错误代码
【发布时间】:2015-02-19 12:56:18
【问题描述】:

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

Parse 在错误对象中返回所有需要的信息,请参阅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)
                    }
                }

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

【问题讨论】:

    标签: ios swift parse-platform


    【解决方案1】:

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

    【讨论】:

    • 当然,没问题!完成:-)
    • 有什么方法可以使用这些常量吗?我真的不喜欢硬编码值...
    • @TakeshiKaga 可以,例如:case NSURLErrorCannotConnectToHost:
    【解决方案2】:

    嗨,我会做类似的事情

    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

    【讨论】:

      【解决方案3】:

      你也可以使用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
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2017-01-16
        • 1970-01-01
        • 1970-01-01
        • 2015-08-21
        • 2014-12-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多