Chris Hansen
Chris Hansen

Reputation: 8685

How do I call a function with the following completion handler?

How do I call a function with the following signature?

func purchaseSubscription(productId: String, completion:  @escaping  (Bool, String, String) -> Void) {

is it

InAppHandler.purchaseSubscription("test") {
     processPurchase()
}

Upvotes: 1

Views: 299

Answers (6)

PGDev
PGDev

Reputation: 24341

There can be multiple forms to call the method.

1. Define the parameters when calling the closure, u.e.

InAppHandler.purchaseSubscription(productId: "test") {(x, y, z) in
    print(x, y, z)
    processPurchase()
}

2. You can use the shorthand form ($0, $1 etc.) for the parameters in the closure while calling it, i.e.

InAppHandler.purchaseSubscription(productId: "test") {
    print($0, $1, $2)
    processPurchase()
}

The above 2 are same. Just that in first one you're giving the parameter names and in the second one you're using the shorthand for those parameters.

3. In case you're not using any parameters that you're getting in the closure, mark them with underscore(_) like so,

InAppHandler.purchaseSubscription(productId: "test") {(_, _, _) in
    processPurchase()
}

You can use any of the other forms depending on your requirement.

Upvotes: 8

vijeesh
vijeesh

Reputation: 1335

Since you are not using any of the closure params it is recommended to use _

  purchaseSubscription(productId: "Id") { _, _, _ in
            processPurchase()
     }

It is good to check the closure param before you do processPurchase()

      purchaseSubscription(productId: "Id") { success, _, _ in
         if success {
               processPurchase()
          }
       }

Upvotes: 1

Rohit Parihar
Rohit Parihar

Reputation: 352

Your call should be like this

 InAppHandler.purchaseSubscription(productId: "YOUR_PRODUCT_ID_STRING") { (boolValue, firstString, secondString) in

 }

Upvotes: 2

YosiFZ
YosiFZ

Reputation: 7900

This is the code you need to use:

InAppHandler.purchaseSubscription(productId: "test") { (boolVal1, stringVal, boolVal2) in
    processPurchase()
}

Upvotes: 2

Jack Goossen
Jack Goossen

Reputation: 1281

The completion handler requires three input parameters. You can ignore the parameters but must be explicit about it. Furthermore the signature indicates this is a member function, while you seem to be calling the function on the type (class, struct, enum...). So the correct way would be:

let inAppHandler = InAppHandler()
inAppHandler.purchaseSubscription(productId: "test") { _, _, _ in 
    processPurchase()
}

Upvotes: 3

Ajjjjjjjj
Ajjjjjjjj

Reputation: 675

this is a your call :

InAppHandler().purchaseSubscription(productId: "anyStringData") { (boolCheck, result1, result2) in
            print(result1)
        }

And this is your defination :

 func purchaseSubscription(productId: String, completion:  @escaping  (Bool, String, String) -> Void) {
    completionResult(true,"Data1", "Data2")
    }

Upvotes: 2

Related Questions