【问题标题】:Retrieve product localised price - Unexpected non-void return value in void function检索产品本地化价格 - 无效函数中的意外非无效返回值
【发布时间】:2018-10-23 21:22:10
【问题描述】:

我正在使用SwiftyStoreKit 在我的项目中实施 iAP。我有一个用户可以购买和恢复的自动更新订阅。这似乎工作正常,但是在检索要显示到 UI 的产品信息时遇到问题。

我正在尝试显示本地化价格,返回的价格是年度成本,因此我需要将该数字除以 12 以将其显示为每月成本。但是,在获取价格并尝试恢复价值时,出现以下错误:

void 函数中出现意外的非 void 返回值

设置按钮值

let subscribeButton = subscriptionManager.getSubscriptionPricePerMonth(isYearly: false)
subscribeButton(monthlyCost, for: .normal)

检索价格

//Get prices

func getSubscriptionPricePerMonth() -> String {
    let productId = getProductId()


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

        if let product = result.retrievedProducts.first {

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

        }
    }

}

【问题讨论】:

    标签: swift in-app-purchase storekit swiftystorekit


    【解决方案1】:

    void 函数中出现意外的非 void 返回值

    错误表明,您正试图从 void 函数返回一些值。

    现在,让我们了解一下您的情况。

    你的实际功能是

    func getSubscriptionPricePerMonth() -> String {}
    

    您希望将一些值作为字符串返回给您。但是看看里面的代码,你使用的是异步块,它有 void 返回类型

    SwiftyStoreKit.retrieveProductsInfo([productId]) { result -> Void in 
      // Here you are returning some values after parsing the data, which is not allowed.
    }
    

    要从块中返回某些东西,您可以使用DispatchGroup 使其同步

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-02-16
      • 1970-01-01
      • 2019-12-19
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 2017-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多