@autorelease or @autoreleasepool?


#1

Hello!

I’m enjoying the book very much (although I might have to work through it again before I REALLY understand everything in it), and as I was rewriting my code to use the key-value coding, I realized that the automatically generated code in my Appliances main.m file was different from the sample in the book.

Here is my code:

[code]int main (int argc, const char * argv[])
{

@autoreleasepool {
    
	Appliance *a = [[Appliance alloc] init];
	NSLog(@"a is %@", a);
	
//	[a setProductName:@"Washing Machine"];
	//above line rewritten to use key-value coding:
	[a setValue:@"Washing Machine" forKey:@"productName"];
	
	
	[a setVoltage:240];
	NSLog(@"a is %@", a);
			 
			 NSLog(@"Hello again, World! The product name is %@", [a valueForKey:@"productName"]);
	
	Appliance *aa = [[Appliance alloc] initWithProductName:@"Toaster Oven"];
	NSLog(@"aa is %@", aa);

[/code]

But the sample code begins like this:

[code]int main (int argc, const char * argv[])
{

@autorelease  {
    
	Appliance *a = [[Appliance alloc] init];
	NSLog(@"a is %@", a);
	[a setValue:@"Washing Machine" forKey:@"productName"];
	[a setVoltage:240];

[/code]

So is that a typo, or is there a difference between @autorelease and @autoreleasepool, and what would that difference be?

I did a cursory search of my Kindle version of the book and found two instances of @autorelease used in code samples:
p. 221, Key-value coding section of Chapter 30
p. 225, Chapter 31

These forums are a great resource!


#2

You have found two typos. I’m so sorry. They should both be @autoreleasepool.

I’ll fix them in the next printing.


#3

No need to apologize! I work at a newspaper, so I know how much work goes into the writing, layout, proofing and production of printed materials – and there are ALWAYS typos, no matter how carefully the writers and editors do their jobs. But with a newspaper, you don’t get a chance for a second edition revision (unless it’s something really egregious; then you print a Correction paragraph in the next issue).