我有以下代码
inAppKeys = [[MKStoreManager sharedManager] purchasableObjectsDescription ];
NSMutableArray * unremovableArray = [[NSMutableArray alloc] init];
for(int i = 0; i<[inAppKeys count]; i++){
for (int j=0; j< [categories count]; j++) {
NSString * inAppKey = [[categories objectAtIndex:j] valueForKey:@"inAppKey"];
if([inAppKey isEqualToString: [inAppKeys objectAtIndex:i]]){
[unremovableArray addObject:[categories objectAtIndex:j]];
}
}
}
categories = [[NSMutableArray alloc] init];
[categories addObjectsFromArray:unremovableArray];
其中category是nsmutablearray ..事情是addObjectsFromArray将类别留空..我做错了什么?
答案 0 :(得分:2)
在你甚至分配/ init类别之前,我认为你指的是[categories count]和[categories objectAtIndex:j]。
重新阅读你的标题(“重新初始化”),这表明你之前已经进入类别,我现在假设你有一组主要的类别,你试图减少到实际购买的类别。如果是这样,我不会重复使用变量“类别”,因为这令人困惑。 (我假设类别是自动释放的,否则你有泄漏)。如何使用unremovableArray而不是泄漏呢?
我还会使用快速枚举器来提高清晰度和速度......
NSLog(@"categories: %@", categories);
inAppKeys = [[MKStoreManager sharedManager] purchasableObjectsDescription ];
NSLog(@"inAppKeys:%@", inAppKeys);
NSMutableArray * unremovableCategories = [[NSMutableArray alloc] init];
for(NSString* thisAppKey in inAppKeys) {
for (NSDictionary* thisCategory in categories) {
if ([[thisCategory valueForKey:@"inAppKey"] isEqualToString: thisAppKey]){
[unremovableCategories addObject:thisCategory];
break; //having added this category; no reason to continue looking at it
}
}
}
//now use unremovableCategories...