Mohamed Emad Hegab
Mohamed Emad Hegab

Reputation: 2675

iphone: addObjectsFromArray not adding after reinitializing nsmutable array

i have the following code

    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];

where categories is nsmutablearray .. the thing is addObjectsFromArray leave the categories empty .. what do i do wrong?

Upvotes: 1

Views: 794

Answers (1)

mackworth
mackworth

Reputation: 5953

Looks to me like you're referring to [categories count] and [categories objectAtIndex:j] before you even alloc/init categories.

Having re-read your title ("reinitializing") which suggests you've previously inited categories, I'm now assuming that you have a master set of categories that you're trying to reduce to the ones actually purchased. If so, I wouldn't re-use the variable "categories" as that's confusing. (I assume categories was auto-released, or else you've got a leak). How 'bout using unremovableArray instead of leaking it?

I'd also use fast enumerators for clarity and speed...

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

Upvotes: 2

Related Questions