Updated for Xcode 14.2
When we call a function that might throw errors, we either call it using try
and handle errors appropriately, or if we’re certain the function will not fail we use try!
and accept that if we were wrong our code will crash. (Spoiler: you should use try!
very rarely.)
However, there is an alternative: if all we care about is whether the function succeeded or failed, we can use an optional try to have the function return an optional value. If the function ran without throwing any errors then the optional will contain the return value, but if any error was thrown the function will return nil. This means we don’t get to know exactly what error was thrown, but often that’s fine – we might just care if the function worked or not.
Here’s how it looks:
enum UserError: Error {
case badID, networkFailed
}
func getUser(id: Int) throws -> String {
throw UserError.networkFailed
}
if let user = try? getUser(id: 23) {
print("User: \(user)")
}
The getUser()
function will always throw a networkFailed
error, which is fine for our testing purposes, but we don’t actually care what error was thrown – all we care about is whether the call sent back a user or not.
This is where try?
helps: it makes getUser()
return an optional string, which will be nil if any errors are thrown. If you want to know exactly what error happened then this approach won’t be useful, but a lot of the time we just don’t care.
If you want, you can combine try?
with nil coalescing, which means “attempt to get the return value from this function, but if it fails use this default value instead.”
Be careful, though: you need to add some parentheses before nil coalescing so that Swift understands exactly what you mean. For example, you’d write this:
let user = (try? getUser(id: 23)) ?? "Anonymous"
print(user)
You’ll find try?
is mainly used in three places:
guard let
to exit the current function if the try?
call returns nil
.SPONSORED From March 20th to 26th, you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.