Home > Back-end >  How can I call this Closure in Swift 5
How can I call this Closure in Swift 5

Time:11-03

I'm still new in Swift development and I'm wondering how can I call This function with the status and results?

 func getStatus(completion: @escaping (Swift.Result<SubscriptionStatus, MAPIError>) -> Void )
{
   getStatus { result in
            switch(result) {
            case .success(let subscription):
                switch(subscription.status) {
                case .subscribed:
                    completion(.success(true))
                default:
                    completion(.success(false))
                }
            case .failure(let error):
                completion(.failure(error))
            }
        }
   }

Many Thanks

Calling the function

CodePudding user response:

Result is an enum which can be either:

success

or

failure

So, completion(.success(value)) or completion(.failure(someError))

CodePudding user response:

Result is such a powerful keyword in swift! It determines wether the task should succeed or fail.

To complete your function you should call it like this:

completion(.success(SubscriptionStatusValue))

If the task succeed

Or

Completion(.failure(error))

To call your function you should do it like this:

self.getStatus { result in 
switch result { 
case .success(let value):
/// do something with value 
case .failure(let error):
///handle error

Hope it helps!!

  • Related