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

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

我原以为“self.data=”会保留自动释放的 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 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;
}

最佳答案

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

一个对象的保留计数的绝对值发生了什么是无关紧要的。

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

关于ios - 将自动释放对象分配给保留属性是否会增加其保留计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15649265/

相关文章:

ios - 按设备重置日期我的应用程序会发生什么情况?

javascript - 这段查找 heightInInches 的代码有什么问题?

ios - View 的 UIPageViewController 框架在 iOS 上是不可预测的

properties - Swift 类 : Property not initialized at super. init 调用中出现错误

apache-flex - 在 Flex 中运行时从文件加载属性

iphone - iOS:如何通过图像/音频生成视频文件?

ios - 基础和 cocoa 框架之间的关系

ios - 重新初始化一个 NSArray

ios - 如何以编程方式移动 UIButton。 objective-C Xcode

c# - 组合属性 setter "init and private"(C#9)