SACC

/ / navigation


/ / Objective C project



  1. Open Xcode and create a new project


  2. Select Command Line Utility under the Mac OS icon on the left


  3. In the right panel select Foundation Tool , then press Choose. Save the project as Hello


  4. Click on your_file.m file. You should see the following:
    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        // insert code here...
        NSLog(@"Hello, World!");
        [pool drain];
        return 0;
    }
    
    
    Delete everything and type in the following:
    #import <Foundation/Foundation.h>
    
    int main (int argc, const char * argv[]) {
         NSLog(@"Hello, World!");
        return 0;
    }
    
    


  5. Build and run (⌘+R) will compile and run the program, but instead press ⌘+⇧+R to compile and run the program in the console window.


Breaking it down

Extensions
The .m extension gets processed by the Objective-C compiler.
The .c extension gets processed by the C compiler.
The .cpp extension gets processed by the C++ compiler.

The convention is to refer to the .m files as "dot-m files"

#import
In C one uses #include to tell the compiler to look at a certain file for some definitions. In Objective-C, use #import. #import guarantees that the header file will be included only once.

The Foundation framework is about 1 megabyte. It contains more than 14,000 lines of code and is comprised of hundreds of files.

NSLog() and @"strings"
NSLog() prints a string to the Console. The method takes a string as its first argument and can contain format specifiers like %d or %f. NSLog() not only prints your string to the console, but it adds a time and date stamp and appends a newline(\n) character at the end of the line.
What is NS all about
The NS prefix is from when the toolkit was called NextSTEP and was a product of NeXT Software. NeXT was bought by Apple in 1996 and instead of rewriting all the code, Apple continued to use the NS prefix.

A string in double quotes preceded by the @ sign means that the quoted string should be treated as a NSString element.

NSString can:
  • Tell you its length
  • Compare itself to a string
  • Convert itself to an integer or floating-point value