2011-09-21 80 views
1

後加入,我有以下代碼iphone:addObjectsFromArray不重新初始化nsmutable陣列

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

,其中,一類是NSMutableArray的..關鍵是addObjectsFromArray離開類別爲空..我該怎麼辦錯了嗎?

+0

你有沒有NSLog的上unremoveableArray?它首先有內容嗎? – Bourne

+0

顯示您的NSLogs – zaph

+0

穆罕默德,我的答案是否適合你? – mackworth

回答

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