2

Possible Duplicate:
Im getting this error ‘autorelease’ is unavailable: not available in automatic reference counting mode

I'm learning Objective-C with a book called Objective-C fundamentals, published 2011. It's building a simple app to introduce iOS concepts and teach the Objective-C language. Seems like there's been some changes to the platform or language since publication of the book. When I try to build code from the book (key passage excerpted below), I'm getting this error:

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

The error message appears a few lines above where autorelease is actually used in the code.

I only have about 1 hour's worth of experience with Objective-C and iOS, so I have no idea how to fix this so I can continue along with the book. Any help would be appreciated.

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

If I can't fix these types of small problems, then I won't be able to follow along with the book. If there's some sort of version system I could use (like rvm for Ruby) to avoid this type of problem, please let me know.

Community
  • 1
  • 1
BrainLikeADullPencil
  • 11,313
  • 24
  • 78
  • 134
  • possible duplicate of [Im getting this error 'autorelease' is unavailable: not available in automatic reference counting mode](http://stackoverflow.com/questions/6363566/im-getting-this-error-autorelease-is-unavailable-not-available-in-automatic-r), http://stackoverflow.com/questions/6692022/why-cant-i-release-an-object-anymore, and http://stackoverflow.com/questions/8236797/xcode-cocoa-release-is-unavailable-error. – CodaFi Dec 27 '12 at 00:38
  • Memory management has changed a lot in iOS5 with the introduction of Automated Reference Counting. You need to read a good introduction to ARC. I recommend [Ray Wenderlich's tutorial](http://www.raywenderlich.com/5677/beginning-arc-in-ios-5-part-1) by Matthijs Hollemans. – Thilo Dec 27 '12 at 00:37

2 Answers2

7

If you are only an hour in, I recommending restarting.

There is a feature to building objective-c projects in Xcode called automatic-reference counting. Your book is not using it.

When you restart, pay close attention when going through the wizard to creating a project. You will need to make sure the

Use Automatic Reference Counting

is not selected.

Jesse Black
  • 7,966
  • 3
  • 34
  • 45
  • I'd rather get a new book. Giving up on ARC seems a bad choice. – Thilo Dec 27 '12 at 00:39
  • 3
    I don't use arc. I learned objective-c before ARC. I think it is best to understand memory-management before using ARC. – Jesse Black Dec 27 '12 at 00:40
  • thanks, this is the answer that will allow me to follow along with the book. I'll restart as described – BrainLikeADullPencil Dec 27 '12 at 00:41
  • @Thilo feel free to recommend another book that I can look at after.Since I already have this one I'll continue with it. – BrainLikeADullPencil Dec 27 '12 at 00:42
  • 1
    "I think it is best to understand memory-management before using ARC." +1 for that. You need that understanding to use ARC properly, too. (but you'd still want to use ARC). – Thilo Dec 27 '12 at 00:42
  • As much as I love your passion for learning, it's really more wise to face facts: The compiler is smarter than every last one of us. Let it do the work for you. – CodaFi Dec 27 '12 at 00:43
  • I think you can read a tutorial on ARC (see my answer) and then continue using your book, adjusting the code as necessary. The changes that have to be made follow a few simple patterns. – Thilo Dec 27 '12 at 00:43
  • 2
    I'd stick to this book. Learning objective-C without ARC is a pretty good way to go. Retain/release memory management is not difficult to learn but important to do right, and easier to learn at the outset than later on. Picking up ARC once you've got the hang of retain/release is easy. ARC doesn't obviate memory management, it just hides it after all - if you don't have a good feel for what's going on underneath you could come unstuck in all sorts of ways. And find a lot of existing code hard to follow, compile, adapt and use in your own projects. – foundry Dec 27 '12 at 00:46
  • @CodaFi except if you're the one who wrote the compiler :) (no, seriously: MRC is necessary, and one **must** learn it before transitioning to ARC.) –  Dec 27 '12 at 00:48
  • @CodaFi I transitioned to ARC. Sometime in the springtime '13 I decided to embrace all the features added to Xcode and target a higher OS. I don't really use storyboards, but even gave them a whirl... I would still assert people learn memory management to avoid retain cycles and other nuances of the memory – Jesse Black Oct 03 '13 at 21:05
2

Read the Advanced Memory Management Guide for iOS. It's a great read and even if you use ARC going forward, it's good to understand. There's still scenarios where you need to understand it.

autorelease is part of manual memory management.

Later in iOS 5, ARC (auto reference counting) was added to avoid having to manually manage memory.

Here's a writeup comparing manual and ARC.

bryanmac
  • 38,941
  • 11
  • 91
  • 99