Optionals vs Result

Here’s the code if you want to experiment with it!

import Foundation

func fetchData(_ completion: @escaping (Result<Data, Error>) -> Void) {
    /* ... */
}

fetchData { result in
    switch result {
    case .success(let data):
        // use the data
    case .failure(let error):
        // handle the error
    }
}
Previous
Previous

Closures and Capture Lists

Next
Next

variadic arguments