Objective-C - Towards our Second iPhone App




CS185c

Chris Pollett

Aug. 31, 2009

Outline

The Basics of an Objective-C Program

Implementation Files

To implement the functions you use an implementation file and the syntax:

@implementation classname
+classMethod {
    // implementation
}
-instanceMethod {
    // implementation
}
@end

Invoking Methods

Example Objective-C Program

// Interface File
#import <objc/Object.h>

@interface MyHello : Object {
   int myNumber;
}
-setNumber:(int)aNumber;
-sayHello;

@end

Example Program cont'd

// implementation file
#import <stdio.h>
#import "MyHello.h" 
@implementation MyHello
-setNumber:(int)aNumber {    myNumber = aNumber;
} 
-sayHello{    
   printf("Hello! %d\n", myNumber);
} 
-(id) init { //self is like this in Java
    self = [super init];
    if (self) { myNumber = 0; }
    return self;}  
@end

Objective-C Example Last Part.

#import "MyHello.h"
int main(void){
MyHello *hello = [MyHello new];  
[hello setNumber:10]; 
   //set the number to echo
[hello sayHello];
return 0;
}
/* To compile in gcc could type:
gcc -x objective-c -Wno-import main.m MyHello.m -lobjc  

You can actually build XCode projects from the command line:
/Developer/usr/bin/xcodebuild -target project
*/

Quiz

Which of the following is true of a phone simulator?

  1. It executes machine code for the target device
  2. It executes machine code for the computer on which you are running it.
  3. It never supports networking.

Objectice-C Types, Foundation Kit

Reference Counting

Starting a Second Project