What does this Objective-C dictionary code do?

穿精又带淫゛_ 提交于 2019-12-02 12:27:09

You are having trouble understanding this line:

IAPProduct * product = _products[skProduct.productIdentifier];

Lets break it down:

NSString *key = skProduct.productIdentifier;
IAPProduct * product = _products[key];

The 2nd line is modern syntax for:

IAProduct * product = [_products objectForKey:key];

This is the normal way to lookup a value in a dictionary for a given key.

It's not IAPProduct *product= NSMutableDictionary[NSArray.productIdentifier];

The type of skProduct is SKProduct, not NSArray. The fast enumeration for (SKProduct * skProduct in skProducts) loops through all elements in skProducts as SKProduct.

It's (to some extent) like using a loop with a counter doing:

//for (SKProduct * skProduct in skProducts)
for (int i=0; i<skProducts.count; i++)
{
    SKProduct *skProduct = skProduct[i];
    IAPProduct * product = _products[skProduct.productIdentifier];
    product.skProduct = skProduct;
    product.availableForPurchase = YES;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!