Chris Mendez in Swift, For Developers

Swift: Do Try Catch

I've discovered that there are really two ways to accomplish a Try/Catch strategy in Swift. The first example is technically correct but the "shorthand" example gives you the same end result.

Longhand

let obj = PFObject(className: "TestObject")  
    obj.setObject("bar", forKey: "foo")

do {  
    try obj.save()
} catch {
    print("error")
}

Shorthand

let obj = PFObject(className: "TestObject")  
    obj.setObject("bar", forKey: "foo")

if let success = try? obj.save(){  
    print("success", success)
} else {
    print("failure")
}