2016-04-23 34 views
0

我創建了一個用戶類,其中包含一個Parse PFUser()對象和一個用於error_messages的字符串。Swift解析在註冊時不顯示錯誤信息

在用戶類中是一個註冊函數,它將使用PFUser對象並執行signUpInBackgroundWithBlock()。

在該函數中,它應該設置一個標誌,通知主視圖控制器發生錯誤,如果執行錯誤消息並從PFUser傳回的錯誤消息中設置User對象中的error_message字符串。

但是,發生錯誤時,函數不會執行完畢,例如,如果輸入了不正確的電子郵件格式,例如aaaa.com而不是[email protected],函數將不會返回並設置標誌相反,從PFUser傳遞的錯誤消息僅顯示在控制檯窗口中。

我已經花了幾天的時間試圖在用戶類中設置標誌和錯誤消息,但我無法弄清楚。

下面是類代碼

class User { 
var user_db_obj = PFUser() 
var error_message = "Please try again later" 
//var user_name: String 
//var pass_word: String 


//Constructor for User object 
init(user: String, pass: String){ 
    user_db_obj.username = user 
    user_db_obj.email = user 
    user_db_obj.password = pass 
} 


//This function signs a user up to the database 
func signUp() -> Bool { 
    var error_in_sign_up: Bool = true 

    user_db_obj.signUpInBackgroundWithBlock {(succeeded: Bool?, error: NSError?) -> Void in 

     //stop spinner and allow interaction events from user 
     activityIndicator.stopAnimating() 
     UIApplication.sharedApplication().endIgnoringInteractionEvents() 

     if error == nil{ 
      error_in_sign_up = false 
      //sign up successful 
     } 
     else{ 
      let error_string = error!.userInfo["error"] as? String 
      self.error_message = error_string! 
     } 
    } 
    if error_in_sign_up == true{ 
     return false 
    } 
    else{ 
     return true 
    } 
} 

這裏是調用從用戶類的註冊功能視圖控制器代碼。

//Action for signup button 
@available(iOS 8.0, *) 
@IBAction func signup_btn(sender: AnyObject) { 

    if email_tf.text!.isEmpty || pass_tf.text!.isEmpty { 
     //if email or password field blank display error message 
     displayAlert("Error in form", msg: "Please enter a username and password") 
    } 

    else{ //perform actual signup/login if email and password supplied 

     //Display spinner while database interaction occuring and ignore user interactions as well 
     activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50)) 
     activityIndicator.center = self.view.center 
     activityIndicator.hidesWhenStopped = true 
     activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray 
     view.addSubview(activityIndicator) 
     activityIndicator.startAnimating() 
     UIApplication.sharedApplication().beginIgnoringInteractionEvents() 

     let theUser = User(user: email_tf.text!, pass: pass_tf.text!) 

     //sign up 
     if signup_mode == true{ 
      if theUser.signUp() == false{ 
       displayAlert("Failed SignUp", msg: theUser.error_message) 
      } 
     } 

     //login 
     else{ 
      if theUser.login() == false{ 
       displayAlert("Failed Login", msg: theUser.error_message) 
      } 
     } 
    } 

} 

回答

0

問題上mainthread該功能signUpInBackgroundWithBlock犯規來看,如果你想保留這個功能,你就必須註冊通知,然後聽時,它的成功與否在其他的viewController ... something like this