Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向UILabel(Swift)(SwiftyStoreKit)显示功能结果_Swift_In App Purchase_Ios10 - Fatal编程技术网

向UILabel(Swift)(SwiftyStoreKit)显示功能结果

向UILabel(Swift)(SwiftyStoreKit)显示功能结果,swift,in-app-purchase,ios10,Swift,In App Purchase,Ios10,我想在我的标签中显示SKProduct项目的价格,而不是像SwiftyStoreKit那样显示alertView 在viewDidLoad中,我尝试了 coralsAppLabel.text = getInfo(PurchaseCorals) 但这导致了一个错误,即我无法将类型()转换为UILabel 这是基于下面的SwiftyStoreKit代码 enum RegisteredPurchase : String { case reefLifeCorals = "ReefLi

我想在我的标签中显示SKProduct项目的价格,而不是像SwiftyStoreKit那样显示alertView

在viewDidLoad中,我尝试了

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

感谢您提供的任何帮助

这里的主要问题是,您试图分配函数
getInfo
隐式返回到
UILabel
String?
属性的
Void
(也称
()
)值。那是行不通的

您也不能轻易地从
getInfo
函数返回所需的信息,因为它执行异步调用。实现所需功能的一种方法是将代码重新分解为以下内容(没有检查语法错误,所以要小心):


在这里,您的
SKProduct
errorMessage
viewDidLoad
的完成闭包中,您可以随意使用它:显示警报、更新标签等。总体而言,此代码应该更加灵活和解耦,这通常是一件好事;)

我对您的代码做了一些更改,以反映Swift 3的要求。另外,我更改了检索到的数据,因为我只需要价格。太棒了。非常感谢。
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)
    }
}