我想知道如果其中一个if语句返回false,如何停止代码,因为我注意到在我的代码中,如果我正在编辑配置文件:例如当用户输入错误的电子邮件时但他的用户名已被接受,我将收到错误电子邮件的UIAlertController,但它会更新用户名,我不想要。有更好的做法吗?谢谢
@IBAction func update(sender: UIButton) {
let user:PFUser = PFUser.currentUser()!
let username = username.text
let email = email.text
var valid:Bool = true
self.view.endEditing(true)
if(valid){
\\ look to find error with mutliple check if inside
valid = false
}else{
user.setObject(emails!, forKey: "email")
}
if(valid){
\\ look to find errorwith mutliple check if inside
valid = false
}else{
user.setObject(usernames!, forKey: "username")
}
if(valid){
let spinner = MBProgressHUD.showHUDAddedTo(self.view, animated:true)
spinner.labelText = "Please Wait"
myUser.saveInBackgroundWithBlock({ (success:Bool, error:NSError?) -> Void in
MBProgressHUD.hideAllHUDsForView(self.view, animated: true)
if(success){
print("awesome")
}else{
self.displayAlert(error!.localizedDescription, dismiss: false)
}
})
}
}

答案 0 :(得分:2)
如果条件失败,您应return
取消处理:
func updateProfile() {
if false == username.isValid() {
// show UIAlertController wrong username format
return
}
if false == email.isValid() {
// show UIAlertController wrong email format
return
}
sendUpdateProfile()
}
上面的代码只是伪代码,你应该自己实现它。
<强> EDITED 强>
请参阅本教程并了解如何使用Parse更新配置文件 http://swiftdeveloperblog.com/update-user-profile-details-with-swift-and-parse/