autorelease在自动引用计数模式下不可用

时间:2012-12-27 00:33:03

标签: objective-c ios memory-management

  

可能重复:
  Im getting this error ‘autorelease’ is unavailable: not available in automatic reference counting mode

我正在学习Objective-C,并出版了一本名为Objective-C基础知识的书,该书于2011年出版。它正在构建一个简单的应用程序来介绍iOS概念并教授Objective-C语言。似乎自本书出版以来,平台或语言发生了一些变化。当我尝试从书中构建代码时(以下摘录的关键段落),我收到了这个错误:

autorelease is unavailable: not available in automatic reference counting mode
ARC forbids explicit message send of 'autorelease'

错误消息显示在上面的几行,其中代码中实际使用了autorelease。

我只有大约1小时的Objective-C和iOS经验,所以我不知道如何解决这个问题,所以我可以继续阅读这本书。任何帮助,将不胜感激。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView
                             dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){                        #### error message here
        cell = [[[UITableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                 reuseIdentifier:CellIdentifier]autorelease];  ### autorelease used here
    }
    cell.textLabel.text = [NSString
                           stringWithFormat:@"Rental Property %d", indexPath.row];
    NSLog(@"Rental Property %d", indexPath.row);
    return cell; 
}

如果我无法解决这些类型的小问题,那么我将无法完成这本书。如果我可以使用某种版本系统(比如rvm for Ruby)来避免这类问题,请告诉我。

2 个答案:

答案 0 :(得分:7)

如果您只有一个小时,我建议重新启动。

在Xcode中构建objective-c项目有一个功能,称为自动引用计数。你的书没有使用它。

重新启动时,请在完成向导的同时密切注意创建项目。你需要确保

  

使用自动参考计数

未被选中。

答案 1 :(得分:2)

阅读适用于iOS的Advanced Memory Management Guide。这是一个很好的阅读,即使你继续使用ARC,也很好理解。您仍然需要了解它。

autorelease是手动内存管理的一部分。

稍后在iOS 5中,添加了ARC(自动引用计数)以避免必须手动管理内存。

这是一篇比较manual and ARC的文章。

相关问题