I am creating an array of dictionaries (called keys) in an iphone application to store section names and line counts to represent a table.
The code is as follows:
[self.results removeAllObjects];
[self.keys removeAllObjects];
NSUInteger i,j = 0;
NSString *key = [NSString string];
NSString *prevKey = [NSString string];
if ([self.allResults count] > 0)
{
prevKey = [NSString stringWithString:[[[self.allResults objectAtIndex:0] valueForKey:@"name"] substringToIndex:1]];
for (NSDictionary *theDict in self.allResults)
{
key = [NSString stringWithString:[[theDict valueForKey:@"name"] substringToIndex:1]];
if (![key isEqualToString:prevKey])
{
NSDictionary *newDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:i],@"count",
prevKey,@"section",
[NSNumber numberWithInt:j],
@"total",nil];
[self.keys addObject:newDictionary];
prevKey = [NSString stringWithString:key];
i = 1;
}
else
{
i++;
}
j++;
}
NSDictionary *newDictionary = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:i],@"count",
prevKey,@"section",
[NSNumber numberWithInt:j],
@"total",nil];
[self.keys addObject:newDictionary];
}
[self.tableview reloadData];
The code works fine the first time, but sometimes I have to rebuild the entire table, so I repeat this code, which is great for the simulator, but on my device the program bombs when I execute the reloadData line:
malloc: *** mmap(size=3772944384) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
malloc: *** mmap(size=3772944384) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Program received signal: "EXC_BAD_ACCESS".
If I delete the reloadData line, the code works on the device.
I am wondering if this is due to the way I built the key array (i.e. using auto-implemented strings and dictionaries).
source
share