0

I'm very new in the world of xcode and Objective- C- Programming. Right now I'm learning programming via "Objective C- Programming: The big Nerd Ranch Guide". Because of an older OSX-Version, I was just able to install xcode 3.2.6. But the book uses the newest xcode version.

while going through the chapters, I faced a problem:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    @autoreleasepool{   
        NSDate *now = [NSDate date];    
        NSLog(@"The date is %@", now);
    }
    return 0;
}

this code sample gives me following error:

"expected expression before @-token"

While searching for a solution in web, I found out that it's a new syntax to xcode 4... I didn't know that there are so major differences between 3.2.6 and the newest version. Now my question: Does that mean all the syntax in xcode 4 has changed to the previous versions and the book is senseless for me? Or is it just this statement? (If yes, how to write in older versions? I even don't know what that statement is good for since I'm a bloody beginner)

Cœur
  • 37,241
  • 25
  • 195
  • 267
Kirinriki
  • 855
  • 4
  • 12
  • 18

4 Answers4

1

You're confusing Xcode (the IDE), with the SDK. The @autorelease pool annotation was added in the iOS 5 SDK, which Xcode 4 happens to give you. If you want this to run in Xcode 3.x you need to make sure you are running it with the iOS 5 SDK.

rooftop
  • 3,031
  • 1
  • 22
  • 33
1

In a word, YES.

Apple, since they pretty much own the entire stack, is free to change the language at whim, and 3.0 to 4.0 have some changes. I really would not waste my time trying to write IOS programs in 3 at this point personally.

The API's for the classes have changed with iOS as well between 3 and 4 and 4 and 5.

I would really suggest, upgrading your Mac to something that will support at least XCODE 4 at this point.

Speckpgh
  • 3,332
  • 1
  • 28
  • 46
1

Replace @autoreleasepool {} with this code:

NSAutoreleasePool *pool = [NSAutoreleasePool new];
NSDate *now = [NSDate date];
NSLog(@"The date is %@", now);
[pool release];
Costique
  • 23,712
  • 4
  • 76
  • 79
  • thanks, this works pretty well! So the solution is to use `NSAutoreleasePool *pool = [NSAutoreleasePool new]; (....) [pool release];` instead of `@autoreleasepool {}`? But I will face more syntax-differences in future, right? – Kirinriki Apr 09 '12 at 18:48
  • There are always differences causing backward compatibility issues when using older SDKs. Fortunately, there aren't many syntactic differences between iOS SDK versions. That said, Apple strongly encourages you to use only the latest and greatest. Unless you really know what you're doing and why, you should install Xcode 4.x. – Costique Apr 09 '12 at 18:56
  • This should be the top answer to this particular question. – Melllvar Jan 04 '13 at 05:59
0

The message you get refers to a new feature of Objective-C that is known as ARC and is meant to simplify memory management. It is available on Apple ObjC compilers starting with Xcode4.

You can still use the book, but you should remove all ARC-related statements (this is not only @autoreleasepool), and in practice it will not be easy because you will also need to add memory management.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • 1
    Actually, `@autoreleasepool {}` isn't specific to ARC, though it was introduced around the same time. Note that there are a number of other language differences that will cause similar problems. OP is in for a rough ride without either buying an old book or [better] upgrading to a modern version of the OS and Xcode. – bbum Apr 09 '12 at 20:47