AshbyEngineer
AshbyEngineer

Reputation: 153

New to Objective-c, Hello World

I am just starting to learn Objective-C programming. I'm developing in Xcode 4.2 on Mac OS X version 10.7.2 on an iMac. I am reading the book "Programming in Objective-C" by Stephen Kochan, which contains a simple "Hello World" example:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSLog (@"Hello, World!");
    [pool drain];
    return 0;
}

It bombs out with lots of errors when compiling:

/Developer/SDKs/MacOSX10.7.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:313:19: error: unknown type name 'NSString' [1]

There are lots more like this. Is there something that needs to be done before compiling for the first time? Some setup in Xcode?

Upvotes: 5

Views: 6762

Answers (3)

MiM
MiM

Reputation: 3

I had this problem too. I found that after I chose "Command line", I selected "Core foundation" rather than "Foundation" in the window where I wrote the project name. This was what caused the error for me. Be careful!

Upvotes: 0

sicKo
sicKo

Reputation: 1256

From your code, it looks like you choose the wrong application project to start with. Seems like you choose something that got to do with c program

I suggest you click File -> new project and choose Cocoa Application to start with.

Then You copy your code and put it inside 'didFinishLaunchingWithOptions' method in your appdelegate file

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    NSLog (@"Hello, World!");
    [pool drain];
    return YES;

}

Build and run the program. You should be able to see your Hello World in your console log..

Upvotes: 1

Daniel
Daniel

Reputation: 31579

Unknown typename NSString means you are passing objective c code to (normal) c compiler

Upvotes: 6

Related Questions