【问题标题】:Handling certain errors in Parse处理 Parse 中的某些错误
【发布时间】:2015-08-28 14:14:41
【问题描述】:

我希望在我的程序中出现某些错误代码时创建某些事件。例如,如果出现错误代码 200,我需要让用户知道他们缺少用户名字段。或者对于错误代码 125,我需要让他们知道他们在创建帐户时没有输入有效的电子邮件地址。如何专门针对这些错误代码?我试过下面的代码没有成功,我做错了什么,这可能吗?

if error.code == 125 {
        var invalidEmail:UIAlertView = UIAlertView(title: Please try again, message: "That does not look like a real email address. Please enter a real one.", delegate: self, cancelButtonTitle: "Try again")
        invalidEmail.show()
    }

xCode 告诉我的错误是我有一个未解决的标识符“错误”。在解析启动项目文件“AppDelegate.swift”中有一个这样的实例,它调用以下代码,它似乎工作得很好。

func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
    if error.code == 3010 {
        println("Push notifications are not supported in the iOS Simulator.")
    } else {
        println("application:didFailToRegisterForRemoteNotificationsWithError: %@", error)
    }
} 

我的代码

@IBAction func signupTapped(sender: AnyObject) {

    let fullname = fullnameField.text
    let email = emailField.text
    let username = usernameField.text
    let password = passwordField.text

    var user = PFUser()
    user.username = username
    user.password = password
    user.email = email
    // other fields can be set just like with PFObject
    user["fullname"] = fullname

    if error.code == 125 {
        let alert = UIAlertController(title: "Please try again", message: "That does not look like a valid email address.", preferedStyle: .Alert)
        alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
        presentViewController(alert, animated: true)
    }

    if fullname.isEmpty || email.isEmpty || username.isEmpty || password.isEmpty {
        let emptyFieldsError:UIAlertView = UIAlertView(title: "Please try again", message: "Please fill out all the fields so that we can create your account.", delegate: self, cancelButtonTitle: "Try again")
        emptyFieldsError.show()
    }

    user.signUpInBackgroundWithBlock {
        (succeeded: Bool, error: NSError?) -> Void in
        if let error = error {
            let errorString = error.userInfo?["error"] as? NSString
            // Show the errorString somewhere and let the user try again.
        } else {
            // Hooray! Let them use the app now.
        }
    }
}

【问题讨论】:

  • 你忘记调用'show'消息了吗?
  • 您似乎没有在您的UIAlertView 上调用show,顺便说一句,这已被弃用。
  • 我已经根据收到的 cmets 对帖子进行了修改。

标签: ios swift parse-platform email-validation


【解决方案1】:

在您的代码所在的位置没有定义error。您只能在 error 存在的范围内引用它。

user.signUpInBackgroundWithBlock {
    (succeeded: Bool, error: NSError?) -> Void in
    if let error = error {
        let errorString = error.userInfo?["error"] as? NSString
        // PUT YOUR ERROR HANDLING CODE HERE
    } else {

    }
}

【讨论】:

    【解决方案2】:

    正如@Larme 已经指出的那样,UIAlertView 已被弃用,因此您应该使用UIAlertController

    if error.code == 125 {
        let alert = UIAlertController(title: "Please try again", message: "That does not look like a valid email address.", preferedStyle: .Alert)
        alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
        presentViewController(alert, animated: true)
    }
    

    【讨论】:

    • 这很有帮助,但我仍然收到一个错误,即 error.code 是未解析的标识符“错误”。
    • @TrentonTyler 你在哪里检查错误?在 Parse 块内?
    • 不,由于这个错误,xCode 没有为 iOS 模拟器构建我的应用程序。所以 xCode 给了我这个错误,但是我在文件中导入 Parse,所以我不确定它为什么会这样做。
    • @TrentonTyler 我的意思是你试图在代码中的哪个位置显示警报
    • @IBAction func signupTapped(sender: AnyObject) {
    猜你喜欢
    • 2017-06-13
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 2012-07-26
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    相关资源
    最近更新 更多