从辅助文件返回另一个文件的值

时间:2018-04-05 10:45:50

标签: swift

我有一个帮助文件,我从函数中获取了正确的值,这是一个扩展名。

extension IAPHelper: SKProductsRequestDelegate
{

    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse)
    {

        self.products = response.products

        for product in response.products
        {

            print(product.price) 

        }

    }


}

我有另一个ViewController,其中有一个表格单元格。我想把这个价格传给那个单元格。我该怎么办?

2 个答案:

答案 0 :(得分:0)

有多种方法可以实现这一目标,但completion handler是我最喜欢的方式。

var completionHandler课程中创建IAPHelper,并在获得您的价值后执行其工作:

在您的通话中声明completionHandler,如下所示:

var completionHandler: (([NSNumber])->Void)?

您可以在SKProductsRequestDelegate的{​​{1}}方法中使用此功能来执行:

IAPHelper

要在另一个ViewController或任何其他类中获取值,请执行以下操作:

var prices :[NSNumber] = []
for product in response.products
{
    print(product.price) 
    prices.append(product.price)
}
if let handler = completionHandler {
   handler(prices)
}

答案 1 :(得分:-1)

在ViewController中包含tableView

for product in response.products {
self.items.append(product.price)
}
tableView.reloadData()
相关问题