New to Objective-c, Hello World

為{幸葍}努か 提交于 2019-12-10 14:33:14

问题


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?


回答1:


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




回答2:


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




回答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!



来源:https://stackoverflow.com/questions/8074579/new-to-objective-c-hello-world

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!