Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 537
AshbyEngineer Avatar asked Nov 10 '11 03:11

AshbyEngineer


2 Answers

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

like image 82
Dani Avatar answered Sep 20 '22 05:09

Dani


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..

like image 25
sicKo Avatar answered Sep 19 '22 05:09

sicKo