-2

我无法理解下面的代码:

for (SKProduct * skProduct in skProducts){
    IAPProduct * product = _products[skProduct.productIdentifier];

。H

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

.m

- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
    _productsRequest = nil;

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

Q1。SKProduct 是一个 Apple StoreKit 类。我们如何能够将 skProducts 数组的元素分配给 SKProduct 的实例?我以为我们只能为属性和 IVAR 赋值?

Q2。我们如何能够创建 IAPProduct 的实例并将其设置为等于 _products[skProduct.productIdentifier],我认为这只是一个 NSString?

如果您想查看 IAAProduct 的 .h 和 .m,请告诉我。谢谢

更新:添加 IAPProduct .h

@class SKProduct;

@interface IAPProduct : NSObject

- (id)initWithProductIdentifier:(NSString *)productIdentifier;
- (BOOL)allowedToPurchase;

@property (nonatomic, assign) BOOL availableForPurchase;
@property (nonatomic, strong) NSString * productIdentifier;
@property (nonatomic, strong) SKProduct * skProduct;
@property (nonatomic, assign) BOOL purchaseInProgress;
@property (nonatomic, assign) BOOL purchase;

@end

.m

@implementation IAPProduct

- (id)initWithProductIdentifier:(NSString *)productIdentifier {
    if ((self = [super init])) {
        self.availableForPurchase = NO;
        self.productIdentifier = productIdentifier;
        self.skProduct = nil;
    }
    return self;
}

- (BOOL)allowedToPurchase {
    if (!self.availableForPurchase) return NO;

    if (self.purchaseInProgress) return NO;

    if (self.purchase) return NO;

    return YES;
}

@end

更新 2:

#import <Foundation/Foundation.h>

typedef void (^RequestProductsCompletionHandler) (BOOL success, NSArray *products);
@interface IAPHelper : NSObject
@property (nonatomic, strong) NSMutableDictionary *products;

-(id)initWithProducts:(NSMutableDictionary *)products;
-(void)requestProductsWithProductIdentifiers:(RequestProductsCompletionHandler)completionHander;

@end

.m

#import "IAPHelper.h"
#import "IAPProduct.h"
#import <StoreKit/StoreKit.h>


@interface IAPHelper () <SKProductsRequestDelegate>
@end

@implementation IAPHelper
{
    SKProductsRequest * _productsRequest;
    RequestProductsCompletionHandler _completionHandler;
}

-(id)initWithProducts:(NSMutableDictionary *)products
{
    if (self = [super init])
    {
        _products = products;
    }
}

- (void)requestProductsWithCompletionHandler:(RequestProductsCompletionHandler)completionHander
{
    // 4
    _completionHandler = [completionHander copy];

    NSMutableSet *productsIdentifiers = [NSMutableSet setWithCapacity:_products.count];
    for(IAPProduct *product in _products.allValues)
    {
        product.availableForPurchase = NO;
        [productsIdentifiers addObject:product.productIdentifier];
    }

    _productsRequest = [[SKProductsRequest alloc]
                        initWithProductIdentifiers:productsIdentifiers];
    _productsRequest.delegate = self;
    [_productsRequest start];
}

#pragma mark - SKProductsRequestDelegate
- (void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{

    NSLog(@"Loaded list of products...");
    _productsRequest = nil;

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

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

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

    _completionHandler(YES, availableProducts);
    _completionHandler = nil;

}

- (void)request:(SKRequest *)request didFailWithError:(NSError *)error
{
    NSLog(@"Failed to load list of products.");
    _productsRequest = nil;
}

@end
4

1 回答 1

2

Q1。SKProduct 是一个 Apple StoreKit 类。我们如何能够将 skProducts 数组的元素分配给 SKProduct 的实例?我以为我们只能为属性和 IVAR 赋值?

不确定我是否正确理解了这个问题。你指的是for (SKProduct * skProduct in skProducts)线吗?在这种情况下,skProduct是一个局部循环变量。它使用快速枚举来遍历数组中的所有元素。循环体将对数组中的每个元素执行一次。在每次迭代中,将数组中的不同元素分配给skProduct(然后在循环体中使用)。

Q2。我们如何能够创建 IAPProduct 的实例并将其设置为等于 _products[skProduct.productIdentifier],我认为这只是一个 NSString?

skProduct.productIdentifier是一个字符串,但它没有分配给IAPProduct变量。该字符串仅用作_products字典中的键(对应products于标题中声明的属性)。你也可以这样写:

IAPProduct * product = [self.products objectForKey:skProduct.productIdentifier];
于 2013-05-16T17:22:17.453 回答