ios - 处理 Parse 中的某些错误

标签 ios swift parse-platform email-validation

我希望在程序中出现某些错误代码时创建某些事件。例如,如果出现错误代码 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.
        }
    }
}

最佳答案

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

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 {

    }
}

关于ios - 处理 Parse 中的某些错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32273154/

相关文章:

ios - 使用 UIScrollView 在两个菜单之间进行转换

ios - UITableViewCell 掩码选择区域/选择区域的大小?

swift - 如何将日期重新格式化为只有年月日? swift

android - 如何使用android parse sdk从解析表中获取随机行(解析对象)?

iphone - replaceItemAtURL :withItemAtURL:backupItemName:options:resultingItemURL:error: broken in iOS 6?

ios - 所有符合协议(protocol)的类都继承默认实现

ios - 在允许运行循环继续的同时延迟的正确方法

ios - 是否可以向 UISlider 拇指图像添加标签?

ios - 如何强制异步保存常量子类?

使用指针快速解析查询