How to handle empty response in Swift 5 Result

Viewed 1862

I have a view controller and a custom class to call APIs. One API doesn't return anything if it succeeds. I get empty response.

class APIManager {
    static func callAPI(completion: @escaping ((Result</*Empty*/, Error>))) {
        completion(.failure(Error()))
        if statusCode == 200 {
            completion(.success(/*Pass nothing*/))
        }
    }
}

I know I can use String type and pass String literal. Is there any better way?

1 Answers

You can use Void as below,

static func callAPI(completion: @escaping (Result<Void, Error>) -> Void) {
     if statusCode == 200 {
          completion(.success(()))
     }
}
Related