1

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?

3
  • Did you NSLog on unremoveableArray? Does it have the contents in the first place? Commented Sep 21, 2011 at 21:30
  • Mohamed, did my answer work for you? Commented Dec 7, 2011 at 20:36
  • sadly no i did a real ugly work around it Commented Dec 9, 2011 at 9:42

1 Answer 1

2

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...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.