2015-08-28 55 views
0

我正在寻找在某些错误代码出现在我的程序中时创建某些事件。例如,如果有错误代码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. 
     } 
    } 
} 
+0

你忘了打电话给'show'消息吗? – tounaobun

+0

你的'UIAlertView'似乎不会调用'show',而这正是不推荐使用的。 – Larme

+0

我已根据收到的意见对我所做的实现进行了更改。 –

回答

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 { 

    } 
} 
0

正如@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) 
} 
+0

这是有帮助的,但我仍然收到错误error.code是一个未解决的标识符'错误'。 –

+0

@TrentonTyler你在哪里检查错误?内部解析块? –

+0

不,xCode不会构建我的iOS模拟器应用程序,因为此错误。所以xCode给我这个错误,但我在文件中导入Parse,所以我不知道它为什么这样做。 –