我會想'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