What does this Objective-C dictionary code do?

前端 未结 2 888
深忆病人
深忆病人 2021-01-28 09:21

.h

@property (nonatomic, strong) NSMutableDictionary * products;  //not synthesized in .m

.m

- (void)productsRequest:(SKProduct         


        
相关标签:
2条回答
  • 2021-01-28 10:11

    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;
    }
    
    0 讨论(0)
  • 2021-01-28 10:23

    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.

    0 讨论(0)
提交回复
热议问题