0

。H

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

.m

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    NSLog(@"Loaded list of products...");
    _productsRequest = nil;

    NSArray * skProducts = response.products;
    for (SKProduct * skProduct in skProducts)
    {
        IAPProduct * product = _products[skProduct.productIdentifier];
        product.skProduct = skProduct;
        product.availableForPurchase = YES;
    }

    for (NSString * invalidProductIdentifier in response.invalidProductIdentifiers)
    {
        IAPProduct * product = _products[invalidProductIdentifier];
        product.availableForPurchase = NO;
        NSLog(@"Invalid product identifier, removing: %@", invalidProductIdentifier);
    }

    NSMutableArray * availableProducts = [NSMutableArray array];
    for (IAPProduct * product in _products.allValues) {
        if (product.availableForPurchase) {
            [availableProducts addObject:product];
        }
    }

    _completionHandler(YES, availableProducts);
    _completionHandler = nil;      
}

我在下面的行中遇到问题:

IAPProduct * product = _products[skProduct.productIdentifier];

我在想我们正在设置类型IAPProduct *product= NSMutableDictionary[NSArray.productIdentifier];

我以为数组只有元素(objectAtIndex)?为什么会有一个点 (.) 和 productIdentifier?

4

2 回答 2

1

它不是IAPProduct *product= NSMutableDictionary[NSArray.productIdentifier];

的类型skProductSKProduct,不是NSArray。快速枚举遍历asfor (SKProduct * skProduct in skProducts)中的所有元素。skProductsSKProduct

这(在某种程度上)就像使用带有计数器的循环一样:

//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;
}
于 2013-05-16T04:04:35.640 回答
1

您无法理解这一行:

IAPProduct * product = _products[skProduct.productIdentifier];

让我们分解一下:

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

第二行是现代语法:

IAProduct * product = [_products objectForKey:key];

这是在字典中为给定键查找值的常规方法。

于 2013-05-16T04:54:22.393 回答