完成处理程序混乱

时间:2018-10-19 01:58:31

标签: ios swift xcode sprite-kit

我目前在我的应用内购买中使用SwiftyStoreKit,而我用来尝试获取价格和产品说明等信息的功能具有补全处理程序,而我是新手,使用补全处理程序并了解有关@escape如果我想返回字符串或让值转义。我的功能代码如下:

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)
}

我现在遇到的问题是,当我想在标签文本中显示该价格时,它要求您提供完成信息

我该怎么办?

enter image description here

1 个答案:

答案 0 :(得分:2)

您需要这样称呼

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

完成地点也需要更改

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 
    }

}