binpress

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.

  1. /* Add the phrase
  2. "I'm ready to program in Objective-C!"
  3. to the output */
  4.  
  5. #import <Foundation/Foundation.h>
  6.  
  7. int main (int argc, const char * argv[])
  8. {
  9.         NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  10.         NSLog (@"Hello, World!");
  11.         NSLog (@"I'm ready to program in Objective-C!");
  12.  
  13.         [pool drain];
  14.         return 0;
  15. }

Build and Run the program. The output should be the same as below:

  1. Hello, World!
  2. 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,

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

Scroll to Top