Hi all,
I have an issue with NSMutableDictionary when re-init with another value?
The problem is I want to create an array or dictionary with key or category name and array of customer and when the loop to next category all the previous objects are gone ...
Any suggestion?
Code:
NSMutableArray *customerList = [[NSMutableArray alloc] init];
NSString *currentCategory = @"";
NSString *nextCategory = @"";
NSMutableArray *customers = [[NSMutableArray alloc] init];
//init the list
NSMutableDictionary *subCategoryList = [[NSMutableDictionary alloc] initWithObjectsAndKeys:nextCategory,@"category_name",customers,@"customers", nil];
while([results next])
{
nextCategory = [results stringForColumn:@"category_name"];
if (![currentCategory isEqualToString:nextCategory]) {
[customers removeAllObjects]; //<< PROBLEM HERE
[subCategoryList removeObjectForKey:@"category_name"]; //<< AND HERE
//add first object to the list
CustomerDetails *customer = [[[CustomerDetails alloc] initWithObject:[results resultDict]] autorelease];
[customers addObject:customer];
//update the category list
[subCategoryList setObject:nextCategory forKey:@"category_name"];
[subCategoryList setObject:customers forKey:@"customers"];
//create item in array
[customerList addObject:subCategoryList];
currentCategory = nextCategory;
}
else
{
//add the rest of objects in the same category
CustomerDetails *customer = [[[CustomerDetails alloc] initWithObject:[results resultDict]] autorelease];
[customers addObject:customer];
[subCategoryList setObject:customers forKey:@"customers"];
}
NSLog(@"%@",customerList);
}