我无法理解下面的代码:
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?
如果你想看看.h和.m代表IAAProduct,请告诉我。感谢
更新:添加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
答案 0 :(得分: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];