skip to Main Content

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

2

Answers


  1. Result is an enum which can be either:

    success

    or

    failure

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

    Login or Signup to reply.
  2. 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!!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search