我有2个类:
NWStorePackage(NW商店包)
NWStorePackageConsumable(可消费的NW商店包)
NWStorePackageConsumable是NWStorePackage的子类(用于不可消耗内容设计)。
最近我将NWStorePackageConsumable添加到项目中。在整个代码中,我都使用NWStorePackage。
我有一个类构造函数来创建正确的实例。
+ (id) storePackageFromStorePackageDictionary: (NSDictionary *) dictionary
根据字典内容,此函数返回一个NWStorePackageConsumable或者NWStorePackage。
当某些操作需要相关消耗品时,我会检查包类型(在NWStorePackage中定义的枚举)。如果该类型设置为可消耗品产品类型,我将将NWStorePackage实例转换为NWStorePackageConsumable实例并调用下面的方法。我想这种类型转换不会成为问题,因为我之前已经正确创建了它们。
- (id) updateConsumableCount: (int) increase;
这会生成以下错误信息:
-[NWStorePackage consumableCount]: unrecognized selector sent to instance 0x9c34380
这里是完整的代码,其中调用了该方法。
for(NWStorePackage *storePackage in _storePackagesArray) {
if ([storePackage purchased]) {
NWStorePackageStorage *sps = [NWStorePackageStorage storePackageStorageWithProductIdentifier:storePackage.productIdentifier andIsPurchased:storePackage.purchased];
if ([storePackage packageType] == StorePackageTypeConsumable) {
NWStorePackageConsumable *consumable = (NWStorePackageConsumable *) storePackage;
[sps setConsumableCount: [consumable consumableCount]];
}
[purchases addObject:sps];
}
}
以下是NWStorePackage的构造函数,我没有在NWStorePackageConsumable中进行重写。
+ (id) initWithContentsOfDictionary: (NSDictionary *) dictionary {
NSLog(@"Initializing NWStorePackage with dictionary content", kLOGLEVEL_STORE);
for (id key in dictionary) {
NSLog(@"Key: %@ :: Value: %@", kLOGLEVEL_STORE, key, [dictionary objectForKey: key]);
}
NWStorePackage *package = [[NWStorePackage alloc] init];
[package setPackageID:[(NSString *)[dictionary objectForKey:@"id"] integerValue]];
[package setTitle:[dictionary objectForKey:@"title"]];
[package setProductIdentifier:[dictionary objectForKey:@"productIdentifier"]];
[package setDescriptionLong:[dictionary objectForKey:@"descriptionLong"]];
[package setDescriptionShort:[dictionary objectForKey:@"descriptionShort"]];
[package setPackageType: (StorePackageType) [[dictionary valueForKey:@"type"] longValue]];
return package;
}
packageType
?直接使用[storePackage isKindOfClass:[NWStorePackageConsumable class]]
不就可以了吗? - Bryan Chen