我会想'self.data ='会保留autorelease NSMutableArray对象和它包含的NSMutableDictionary对象,但是当表格的cellForRowAtIndexPath方法试图访问self.data中的NSDictionaries时,我最终得到EXC_BAD_ACCESS。是否将autorelease对象分配给保留属性会增加其保留数?
@property (strong, nonatomic) NSMutableArray *data;
- (void) updateReceivedData:(NSData *) jsonData
{
NSMutableArray *fetchedData = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
self.data = [self convertDates:fetchedData withFormat:kMySQLDateTimeFormat];
[self.tableView reloadData];
}
}
- (NSMutableArray*) convertDates:(NSMutableArray *) array withFormat:(NSString *) format
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:format];
NSMutableArray *newArray = [NSMutableArray arrayWithArray:array];
for (NSMutableDictionary *dict in newArray)
{
for (id key in dict.allKeys)
{
if ([[dict objectForKey:key] isKindOfClass:[NSString class]])
{
NSString *value = [dict objectForKey:key];
NSDate *date = [dateFormatter dateFromString:value];
if (date) [dict setObject:date forKey:key];
}
}
}
[dateFormatter release];
return newArray;
}
BAD_ACCESS HERE在NSLog之间抛出这里。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"cell";
CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
NSLog (@"Cell was nil");
cell = [[[CustomCell alloc] init] autorelease];
}
NSDictionary *dict = [[NSDictionary alloc] init];
if (_isFiltered){
dict = [self.filteredData objectAtIndex:indexPath.row];
} else {
dict = [self.data objectAtIndex:indexPath.row];
}
NSLog (@"Filling Label 1");
cell.IDLabel.text = [[dict objectForKey:@"Id"] stringValue];
NSLog (@"Filling Label 2");
cell.firstNameLabel.text = [dict objectForKey:@"firstName"];
[dict release];
return cell;
}
在什么行会引发异常? – 2013-03-26 23:34:55
添加'-cellForForAtIndexPath'代码来提问。 – TijuanaKez 2013-03-26 23:50:45
假设您在属性声明中使用'strong',那么'self.data = foo'将保留该对象。但是,这并没有阻止其他代码在太多时间释放它。最有可能的事情应该释放对象*一次*实际上释放它*两次*。我建议启用ARC而不是尝试查找问题,这很容易。 – 2013-03-27 00:57:42