Learn Objective-C: Extending “Hello, World!”
Now that you understand how the “Hello World” program works, let’s add the phrase “I’m ready to program in Objective-C!” to the output. Simply add another NSLog statement to the program. The solution is below, but try to figure out the problem for yourself.
/* Add the phrase
"I'm ready to program in Objective-C!"
to the output */
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSLog (@"Hello, World!");
NSLog (@"I'm ready to program in Objective-C!");
[pool drain];
return 0;
}
Build and Run the program. The output should be the same as below:
Hello, World!
I'm ready to program in Objective-C!
The NSLog calls automatically create a paragraph break, or a new line, between subsequent calls. It does this automatically, but you can also force a new line in the middle of an NSLog call by using \n anywhere in the line of output. Therefore,
NSLog (@"Hello, World! \nI'm ready to program in Objective-C!");
Produces the same output as above.
This post is part of the Learn Objective-C in 24 Days course.
Author: Feifan Zhou