Before Swift 3 I was using:
guard let data = Data(contentsOf: url) else {
print("There was an error!)
return
}
However I now have to use do
, try
and catch
. I'm not familiar with this syntax. How would I replicate this behaviour?
The difference here is that
Data(contentsOf: url)
does not return an Optional anymore, it throws.So you can use it in Do-Catch but without
guard
:Note that you could still use
guard
withtry?
instead oftry
but then the possible error message is ignored. In this case, you don't need a Do-Catch block: