A standard do-catch block looks like this in Swift:
let jsonEncoder = JSONEncoder()
do {
let file = try jsonEncoder.encode(pets)
} catch {
return
}
// want to access file here
My question is what is the best practice for accessing the variable created inside the do-catch block? My instinct says to first create the variable outside the block as a unwrapped optional (let file: Data!
) but it doesn't feel very elegant. Is there a better way to do this?