I'm trying to check when I save an object to my parse serve if something goes wrong. But I have two options and I have three options and I don't know the difference. I have these three options (if error == nil, or if object != nil, or if error == nil and object != nil). Which one should I use. Thanks
Option #1
let user = PFUser.current()!
user.saveInBackground (block: { (success:Bool, error:Error?) -> Void in
if error == nil{
}
)}
Option #2
let user = PFUser.current()!
user.saveInBackground (block: { (success:Bool, error:Error?) -> Void in
if object != nil{
}
)}
Option #3
let user = PFUser.current()!
user.saveInBackground (block: { (success:Bool, error:Error?) -> Void in
if error == nil && object != nil{
}
)}
I suggest and encourage you to use guard statement, which as made for such situations.
//Always safely unwrap optional value:
if let user = PFUser.current(){
user.saveInBackground (block: { (success:Bool, error:Error?) -> Void in
guard success, error == nil else {
//handle error somehow...(print or whatever...)
return
}
//Continue here as everything is fine...
)}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments