Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle empty response in Swift 5 Result

Tags:

swift

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?

like image 504
iOSDev Avatar asked Jan 26 '23 17:01

iOSDev


1 Answers

You can use Void as below,

static func callAPI(completion: @escaping (Result<Void, Error>) -> Void) {
     if statusCode == 200 {
          completion(.success(()))
     }
}
like image 79
Kamran Avatar answered Jan 31 '23 10:01

Kamran