Antonio Labra
Antonio Labra

Reputation: 2000

Return Callback inside Callback Swift

I have a SDK integration that returns a response using a completion but I want to create another completion to return the callback response, but I don't know how.

This is my attempt to do that

func validatingProcces(completion: ((Any)?)->()) {
    let conekta = Conekta()
    conekta.delegate = self
    conekta.publicKey = "key_KJysdbf6PotS2ut2"
    conekta.collectDevice()
    let card = conekta.card()
    let token = conekta.token()
    
    card!.setNumber(String(cardToSave!.cardNumber), name: cardToSave!.fullName, cvc: String(cardToSave!.cvc), expMonth: String(cardToSave!.month), expYear: String(cardToSave!.year))

    token!.card = card
    
   

    token!.create(success: { (data) -> Void in
        completion(data as Any)
        
    }, andError: { (error) -> Void in
        print(error as Any)
        completion(error as Any)
    })
    
}

I have the following error:

Escaping closure captures non-escaping parameter 'completion'

and also:

  1. Parameter 'completion' is implicitly non-escaping
  2. Captured here

Ps. You'll find the SDK integration here: https://github.com/conekta/conekta-ios

Thank you so much!

Upvotes: 0

Views: 376

Answers (1)

Kyle Burns
Kyle Burns

Reputation: 367

From the source code it looks like you could just make a callback like this:

completion: @escaping (Any?, Error?) -> ()

and pass in the result of the api callback so you can handle it elsewhere like this

token!.create(success: { data in
    completion(data, nil)
}, andError: { error in
    print(error as Any)
    completion(nil, error)
})

Let me know if this works

Upvotes: 1

Related Questions