Invert guard let scoping

Instead of that weird double-guard, I would suggest force-unwrapping the variable just once:

        guard error == nil else {
            let error = error!
            log("ERROR: \(error)")
            completion(.failure(error))
            return
        }

In my view, there is no problem in using force-unwrap right after you’ve made sure that the variable is not nil, where a reader of the code can see that the unwrapping is fully justified and will not lead to a crash.
I even tend to find this approach more legible than if let or guard let, thanks to the unwrapping being explicit rather than implicit.

3 Likes