Objective-C - Starting Our Second iPhone App




CS185c

Chris Pollett

Sep. 1, 2010

Outline

Finish Android Demo

AVD Screenshot of Hello3 app

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 -arch i386 -x objective-c -Wno-import main.m MyHello.m -lobjc  

To get this to work on 64 bit architecture need to switch to NSObject...
You can actually build XCode projects from the command line:
/Developer/usr/bin/xcodebuild -target project
*/

Objectice-C Types, Foundation Kit

Reference Counting

Starting a Second Project

A Second Project

Declaring Actions and Interfaces in Our Header