Astrum
Astrum

Reputation: 375

Completion handler confusion

i'm currently using SwiftyStoreKit for my in app purchases and the function i'm using to try and get the information such as price and product description has a completion handler and I'm new to using completion handlers and read about the @escape if I want to return a string or let a value escape. My function code is as follows:

func getPrice(product: IAPProducts, completion: @escaping (String) -> Void) {
    var priceString = ""
    SwiftyStoreKit.retrieveProductsInfo(["Grant.Marco.1000Coins"]) { result in
        if let product = result.retrievedProducts.first {
            priceString = product.localizedPrice!
            print("Product: \(product.localizedDescription), price: \(priceString)")
        }
        else if let invalidProductId = result.invalidProductIDs.first {
            print("Invalid product identifier: \(invalidProductId)")
        }
        else {
            print("Error: \(String(describing: result.error))")
        }
    }
    completion(priceString)
}

The problem I have now is when I want to display that price in my label text it asks for completion information

What do I put of for it?

enter image description here

Upvotes: 1

Views: 85

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100549

You need to call it like this

SwiftyStoreKitController.shared.getPrice(product:IAPProducts.thousand) { (price) in
   // set here 
   let label = SKLabelNode(text:price)
}

Also completion place need a change

func getPrice(product: IAPProducts, completion: @escaping (String) -> Void) {
    var priceString = ""
    SwiftyStoreKit.retrieveProductsInfo(["Grant.Marco.1000Coins"]) { result in
        if let product = result.retrievedProducts.first {
            priceString = product.localizedPrice!
            print("Product: \(product.localizedDescription), price: \(priceString)")
        }
        else if let invalidProductId = result.invalidProductIDs.first {
            print("Invalid product identifier: \(invalidProductId)")
        }
        else {
            print("Error: \(String(describing: result.error))")
        }

        completion(priceString)  // << here 
    }

}

Upvotes: 2

Related Questions