Ios 将自动释放对象指定给保留的属性是否会增加其保留计数?

Ios 将自动释放对象指定给保留的属性是否会增加其保留计数?,ios,objective-c,properties,exc-bad-access,retain,Ios,Objective C,Properties,Exc Bad Access,Retain,我本以为“self.data=”会保留autorelease NSMutableArray对象及其包含的NSMutableDictionary对象,但最终当表的CellForRowatinedExath方法尝试访问self.data中的NSDictionary时,我获得了EXC_BAD_访问权 @property (strong, nonatomic) NSMutableArray *data; - (void) updateReceivedData:(NSData *) jsonData {

我本以为“self.data=”会保留autorelease NSMutableArray对象及其包含的NSMutableDictionary对象,但最终当表的CellForRowatinedExath方法尝试访问self.data中的NSDictionary时,我获得了EXC_BAD_访问权

@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;
}
NSLogs之间抛出了错误的\u访问

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

打开zombies,看看它是否发现了问题(EXC_BAD_ACCESS不一定意味着过度释放对象,但它可能会)

对象的retain count的绝对值发生了什么变化与此无关


但是,如果通过setter(即self.data=…而不是_data=…)进行赋值,则
strong
属性意味着对象将被保留。

为什么要在cellForRowAtIndexPath:中释放dict。即使您分配dict,您也在分配另一个指针,它是来自filteredData或data的对象。只需删除[data release],并在声明数据时将其分配为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;
}

在哪一行引发异常?向问题添加了“-cellForForAtIndexPath”代码。假设您在属性声明中使用了
strong
,则
self.data=foo
将保留该对象。然而,这并不能阻止其他地方的代码多次发布它。最有可能的是,应该释放对象一次的东西实际上是释放对象两次。我建议启用ARC,而不是试图找到问题,这更容易。这正是问题所在。您正在释放的
dict
对象与您分配的对象不同。谢谢,我实际上刚刚算出了这一点,为您的解释干杯。最初我将其创建为“NSDictionary*dict=[self.data objectAtIndex:indexPath.row];”但当我添加if语句时,编译器说“dict”未声明。我忘了申报为零。我想从技术上来说,bbum是这个问题的正确答案。感谢恩孔加拉发现了我的代码错误。然而,这是正确的答案。