这个Objective-C词典代码有什么作用? [英] What does this Objective-C dictionary code do?

查看:99
本文介绍了这个Objective-C词典代码有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.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;      
}

我在下面的行上遇到了麻烦

I'm having trouble with the line below:

IAPProduct * product = _products[skProduct.productIdentifier];

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

I'm thinking we are setting Type IAPProduct *product= NSMutableDictionary[NSArray.productIdentifier];

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

I thought Array's only have elements (objectAtIndex)? Why is there a dot (.) and the productIdentifier?

推荐答案

您在理解这一行时遇到了麻烦:

You are having trouble understanding this line:

IAPProduct * product = _products[skProduct.productIdentifier];

让我们分解一下:

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.

这篇关于这个Objective-C词典代码有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆