我想在我的标签里显示SKProduct产品的价格,而不是像SwiftyStoreKit所展示的那样作为alertView。
在视野中,我试着

coralsAppLabel.text = getInfo(PurchaseCorals)

但这会导致错误,即我无法将类型()转换为UILabel。
这是基于下面的SwiftyStoreKit代码。
enum RegisteredPurchase : String {

case reefLifeCorals         = "ReefLife4Corals"
}


@IBOutlet weak var coralsAppLabel: UILabel!


func getInfo(_ purchase: RegisteredPurchase) {

    NetworkActivityIndicatorManager.networkOperationStarted()
    SwiftyStoreKit.retrieveProductsInfo([purchase.rawValue]) { result in
        NetworkActivityIndicatorManager.networkOperationFinished()

        self.showAlert(self.alertForProductRetrievalInfo(result))
    }
}

func alertForProductRetrievalInfo(_ result: RetrieveResults) -> UIAlertController {

    if let product = result.retrievedProducts.first {
        let priceString = product.localizedPrice!
        return alertWithTitle(product.localizedTitle, message: "\(product.localizedDescription) - \(priceString)")
    }
    else if let invalidProductId = result.invalidProductIDs.first {
        return alertWithTitle("Could not retrieve product info", message: "Invalid product identifier: \(invalidProductId)")
    }
    else {
        let errorString = result.error?.localizedDescription ?? "Unknown error. Please contact support"
        return alertWithTitle("Could not retrieve product info", message: errorString)
    }
}

如有任何帮助,我们将不胜感激

最佳答案

这里的主要问题是,您试图分配Void(也称为())值,您的函数getInfo隐式返回到String?UILabel属性。那不行。
您也不能很容易地从getInfo函数返回所需的信息,因为它执行异步调用。实现所需功能的一种方法是将代码重新分解为如下内容(不要检查语法错误,所以要小心):

override func viewDidLoad() {
    super.viewDidLoad()

    getProductInfoFor(PurchaseCorals, completion: { [weak self] (product, errorMessage) in
        guard let product = product else {
            self?.coralsAppLabel.text = errorMessage
            return
        }

        let priceString = product.localizedPrice!
        self?.coralsAppLabel.text = "\(product.localizedDescription) - \(priceString)"
    })
}

func getProductInfoFor(_ purchase: RegisteredPurchase, completion: (product: SKProduct?, errorMessage: String?) -> Void) {
    NetworkActivityIndicatorManager.networkOperationStarted()
    SwiftyStoreKit.retrieveProductsInfo([purchase.rawValue]) { result in
        NetworkActivityIndicatorManager.networkOperationFinished()

        let extractedProduct = self.extractProductFromResults(result)
        completion(product: extractedProduct.product, errorMessage: extractedProduct.errorMessage)
    }
}

func extractProductFromResults(_ result: RetrieveResults) -> (product: SKProduct?, errorMessage: String?) {
    if let product = result.retrievedProducts.first {
        return (product: product, errorMessage: nil)
    }
    else if let invalidProductId = result.invalidProductIDs.first {

        return (product: nil, errorMessage: "Invalid product identifier: \(invalidProductId)")
    }
    else {
        let errorString = result.error?.localizedDescription ?? "Unknown error. Please contact support"
        return (product: nil, errorMessage: errorString)
    }
}

在这里,你在完成关闭时在SKProduct中有你的errorMessageviewDidLoad,你可以自由地用它做任何你想做的事情:显示警报,更新标签等等,总的来说,这个代码应该有一点灵活性和解耦,这通常是一件好事。

10-08 05:42