2012-12-27 27 views
2

Possible Duplicate:
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' 

錯誤消息的幾行以上,其中自動釋放在代碼中實際使用。我只有大約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)來避免這種類型的問題,請讓我知道。

+0

[我得到這個錯誤'autorelease'的可能重複是不可用的:不適用於自動引用計數模式](http://stackoverflow.com/questions/6363566/im-getting-this-error-autorelease-is-不可用,不可用在自動r),http://stackoverflow.com/questions/6692022/why-cant-i-release-an-object-anymore,和http://stackoverflow.com/questions/ 8236797/Xcode的可可釋放 - 是 - 不可用錯誤。 – CodaFi

+0

隨着引入自動引用計數,內存管理在iOS5中發生了很大變化。你需要閱讀一篇關於ARC的好介紹。我推薦Matthijs Hollemans撰寫的[Ray Wenderlich的教程](http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1)。 – Thilo

回答

7

如果你只有一個小時,我建議重新啓動。

有一個功能可以在Xcode中構建名爲自動引用計數的objective-c項目。你的書沒有使用它。

當您重新啓動時,請在通過嚮導創建項目時密切注意。您需要確保沒有選擇

Use Automatic Reference Counting

+0

我寧願拿一本新書。放棄ARC似乎是一個不錯的選擇。 – Thilo

+3

我不使用弧。我在ARC之前學習了objective-c。我認爲最好在使用ARC之前瞭解內存管理。 –

+0

謝謝,這是答案,可以讓我跟隨這本書。我會按照說明重新開始 – BrainLikeADullPencil

2

閱讀適用於iOS的Advanced Memory Management Guide。這是一個很棒的閱讀,即使你使用ARC,也很好理解。仍然有需要了解它的場景。

autorelease是手動內存管理的一部分。

後來在iOS 5中,添加了ARC(自動引用計數)以避免必須手動管理內存。

這是一個比較manual and ARC的文章。