Search code examples
objective-cxcode4nslognsautoreleasepool

Beginner: Difference between Xcode 3 and Xcode 4 specifically NSfunctions


I'm learning objc and Xcode from a handy free ebook called How To Become and Xcoder, which is quite handy. Except it was written in 2007 with Xcode 3 and its samples are all from that version unfortunately I have OSX Lion and thus Xcode 4. So to the grit of my question. They provide a sample block of code as seen here:

//start
#import <Foundation/Foundation.h
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(@"Hello, World!");
[pool drain];
return 0;
}
//end

So my problems are that I get over 20 errors and Xcode doesn't recognize the NSAutoreleasePool or NSLog commands. Does anybody know why this won't work? I already added the Foundation framework. The I've already realized that the printf command works better than the NSLog command (which to my knowledge is more so used for error reporting) so yeah any help would be nice.


Solution

  • If NSLog isn't being recognised, this isn't a problem to do with Xcode 3 versus Xcode 4. Your code isn't including the most basic Objective-C runtime code. I would assume that it's being compiled as C not Objective-C.

    You say that you had to add the Foundation framework - this indicates that you didn't start your project using an Objective-C-based template. When you select New Project..., which template did you pick? Any of the iOS application templates should work, and most of the Mac OS X application templates should work. If you chose to build a command line tool, you should pick Foundation as the type.

    Edit: Also, I should add that the syntax for autorelease pools has changed in the latest version of Xcode, as ARC is used by default. You can either switch ARC off, or read up on it to see what the differences are. Chances are you'll find it easier with ARC as there is much less memory management for you to worry about, but you will have to bear it in mind if you are following a book that doesn't account for it.