将自动释放对象分配给保留属性会增加其保留计数吗?

时间:2013-03-26 23:25:39

标签: ios objective-c properties exc-bad-access retain

我原以为'self.data ='会保留autorelease NSMutableArray对象及其包含的NSMutableDictionary对象,但最终当表的cellForRowAtIndexPath方法尝试访问self.data中的NSDictionaries时,我得到EXC_BAD_ACCESS。

@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在这里抛出NSLogs。

- (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;
}

2 个答案:

答案 0 :(得分:5)

打开僵尸并查看它是否能解决问题(EXC_BAD_ACCESS并不一定意味着过度释放的对象,但可能会这样)。

对象保留计数的绝对值会发生什么变化无关紧要。

但是,strong属性意味着保留对象,是的,如果通过setter分配(即self.data = ...而不是_data = ...)。

答案 1 :(得分:2)

为什么要在cellForRowAtIndexPath中发布dict :.虽然你分配了dict,但你正在分配另一个指针,它是来自filteredData或data的对象。只需删除[数据发布],并在声明数据时将其指定为nil

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

    // **As you will be assigning the object from filteredData/data, don't allocate here**
    NSDictionary *dict = nil;
    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"];
    // **Release not required as you didn't allocate**
    //[dict release];
    return cell;
}