Objective-C Beginner's Guide

Embed Size (px)

Text of Objective-C Beginner's Guide

Objective-C Beginner's Guide

08-9-1 8:56

Translations: English | Chinese

hello world Classes @interface @implementation ... Constructors Class level access Exceptions Inheritance, Polymorphism id Inheritance Dynamic types Categories Posing Protocols Retain and Release Dealloc Autorelease Pool Foundation Framework Classes NSArray NSDictionary

objc.tar.gz Steve Kochan Programming in Objective-C.

Linux/FreeBSD: GNUStep GNUstep /usr/GNUstep/System/Makefiles/GNUstep.sh GNUstep.sh /usr, some /usr/lib /usr/localhttp://www.otierney.net/objective-c.html.zh-tw.big5#downloading 1 34

Objective-C Beginner's Guide

08-9-1 8:56

shell csh/tcsh shell GNUStep.csh .bashrc .cshrc Mac OS X: XCode Windows NT 5.X: cygwin mingw GNUStep

C C C K&R The C Programming Language C C Objective-C C C C nil C/C++ NULL Objective-C nil nil [nil message]; NULL BOOLC Objective-C Foundation classes import NSObject.hnil BOOL Objective-C YES NO TRUE FALSE #import vs #include hello world #import#import gcc #include#import .h #ifndef #define #endif #import gcc Objective-C Apple #import Apple #import gcc Objective-C method message messages message Objective-C

hello worldhello.m#import int main( int argc, const char *argv[] ) { printf( "hello world\n" ); return 0; }

hello world

Objective-C #import #include Objective-C .m

classes@interfacehttp://www.otierney.net/objective-c.html.zh-tw.big5#downloading 2 34

Objective-C Beginner's Guide

08-9-1 8:56

"Programming in Objective-C," Copyright 2004 by Sams Publishing Fraction.h#import @interface Fraction: NSObject { int numerator; int denominator; } -(void) print; -(void) setNumerator: (int) d; -(void) setDenominator: (int) d; -(int) numerator; -(int) denominator; @end

NSObjectNeXTStep Object OpenStep inheritance Class: Parent Fraction: NSObject @interface Class: Parent { .... } instance variables protected, public, private protected Instance methods instance variablesscope (returnType) methodName: (parameter1Type) parameter1Name; scope class instance instance methods - class level methods + Interface @end

@implementation "Programming in Objective-C," Copyright 2004 by Sams Publishing Fraction.m#import "Fraction.h" #import @implementation Fraction -(void) print { printf( "%i/%i", numerator, denominator ); } -(void) setNumerator: (int) n { numerator = n; } -(void) setDenominator: (int) d { denominator = d; } -(int) denominator { return denominator; } -(int) numerator { return numerator; } @end

Implementation @implementation ClassName @end http://www.otierney.net/objective-c.html.zh-tw.big5#downloading 3 34

Objective-C Beginner's Guide

08-9-1 8:56

Implement methods interface

"Programming in Objective-C," Copyright 2004 by Sams Publishing main.m#import #import "Fraction.h" int main( int argc, const char *argv[] ) { // create a new instance Fraction *frac = [[Fraction alloc] init]; // set the values [frac setNumerator: 1]; [frac setDenominator: 3]; // print it printf( "The fraction is: " ); [frac print]; printf( "\n" ); // free memory [frac release]; return 0; }

outputThe fraction is: 1/3

Fraction *frac = [[Fraction alloc] init]; Objective-C methods [object method] C++ object->method() Objective-C value C++ Fraction frac; frac.print(); Objective-C [Fraction alloc] Fraction class alloc method malloc [object init] constructor [Fraction alloc] instance init method Object *var = [[Object alloc] init]; [frac setNumerator: 1] frac setNumerator method 1 C Objective-C release NSObject method

... Smalltalk "Programming in Objective-C," Copyright 2004 by Sams Publishinghttp://www.otierney.net/objective-c.html.zh-tw.big5#downloading 4 34

Objective-C Beginner's Guide

08-9-1 8:56

Fraction.h... -(void) setNumerator: (int) n andDenominator: (int) d; ...

Fraction.m... -(void) setNumerator: (int) n andDenominator: (int) d { numerator = n; denominator = d; } ...

main.m#import #import "Fraction.h" int main( int argc, const char *argv[] ) { // create a new instance Fraction *frac = [[Fraction alloc] init]; Fraction *frac2 = [[Fraction alloc] init]; // set the values [frac setNumerator: 1]; [frac setDenominator: 3]; // combined set [frac2 setNumerator: 1 andDenominator: 5]; // print it printf( "The fraction is: " ); [frac print]; printf( "\n" ); // print it printf( "Fraction 2 is: " ); [frac2 print]; printf( "\n" ); // free memory [frac release]; [frac2 release]; return 0; }

outputThe fraction is: 1/3 Fraction 2 is: 1/5

method setNumerator:andDenominator: method:label1:label2:label3: [obj method: param1 label1: param2 label2: param3 label3: param4] Labels methodmethod::: label :

Constructors http://www.otierney.net/objective-c.html.zh-tw.big5#downloading 5 34

Objective-C Beginner's Guide

08-9-1 8:56

"Programming in Objective-C," Copyright 2004 by Sams Publishing Fraction.h... -(Fraction*) initWithNumerator: (int) n denominator: (int) d; ...

Fraction.m... -(Fraction*) initWithNumerator: (int) n denominator: (int) d { self = [super init]; if ( self ) { [self setNumerator: n andDenominator: d]; } return self; } ...

main.m#import #import "Fraction.h" int main( int argc, const char *argv[] ) { // create a new instance Fraction *frac = [[Fraction alloc] init]; Fraction *frac2 = [[Fraction alloc] init]; Fraction *frac3 = [[Fraction alloc] initWithNumerator: 3 denominator: 10]; // set the values [frac setNumerator: 1]; [frac setDenominator: 3]; // combined set [frac2 setNumerator: 1 andDenominator: 5]; // print it printf( "The fraction is: " ); [frac print]; printf( "\n" ); printf( "Fraction 2 is: " ); [frac2 print]; printf( "\n" ); printf( "Fraction 3 is: " ); [frac3 print]; printf( "\n" ); // free memory [frac release]; [frac2 release]; [frac3 release]; return 0; }

outputThe fraction is: 1/3 Fraction 2 is: 1/5http://www.otierney.net/objective-c.html.zh-tw.big5#downloading 6 34

Objective-C Beginner's Guide

08-9-1 8:56

Fraction 3 is: 3/10

@interface @implementation super JavaObjective-C parent class [super init] Super constructor instance selfSelf C++ Java this if ( self ) ( self != nil ) super constructor nil Objective-C C/C++ NULL NSObject self -(id) init Objective-C "init" method C++ Java

@protected Java methods public/private/protected Objective-C C++ instance variableC++ data members Access.h#import @interface Access: NSObject { @public int publicVar; @private int privateVar; int privateVar2; @protected int protectedVar; } @end

Access.m#import "Access.h" @implementation Access @end

main.m#import "Access.h" #import int main( int argc, const char *argv[] ) { Access *a = [[Access alloc] init]; // works a->publicVar = 5; printf( "public var: %i\n", a->publicVar ); // doesn't compile //a->privateVar = 10; //printf( "private var: %i\n", a->privateVar ); [a release];http://www.otierney.net/objective-c.html.zh-tw.big5#downloading 7 34

Objective-C Beginner's Guide

08-9-1 8:56

return 0; }

outputpublic var: 5

C++ private: [list of vars] public: [list of vars] @private, @protected,

Class level access instance class level variables class level functions ClassA.h#import static int count; @interface ClassA: NSObject +(int) initCount; +(void) initialize; @end

ClassA.m#import "ClassA.h" @implementation ClassA -(id) init { self = [super init]; count++; return self; } +(int) initCount { return count; } +(void) initialize { count = 0; } @end

main.m#import "ClassA.h" #import int main( int argc, const char *argv[] ) { ClassA *c1 = [[ClassA alloc] init]; ClassA *c2 = [[ClassA alloc] init]; // print count printf( "ClassA count: %i\n", [ClassA initCount] ); ClassA *c3 = [[ClassA alloc] init]; // print count again printf( "ClassA count: %i\n", [ClassA initCount] ); [c1 release];http://www.otierney.net/objective-c.html.zh-tw.big5#downloading 8 34

Objective-C Beginner's Guide

08-9-1 8:56

[c2 release]; [c3 release]; return 0; }

outputClassA count: 2 ClassA count: 3

static int count = 0; class variable Java static class variables +(int) initCount; count method type - + + class level function class level functions class functions class method ClassA count++ +(void) initialize method is Objective-C class count class level variables

Exceptions Mac OS X 10.3 "Programming in Objective-C," Copyright 2004 by Sams Publishing CupWarningException.h#import @interface CupWarningException: NSException @end

CupWarningException.m#import "CupWarningException.h" @implementation CupWarningException @end

CupOverflowException.h#import @interface CupOverflowException: NSException @end

CupOverflowException.m#import "CupOverflowException.h" @implementation CupOverflowException @end

Cup.h#import

http://www.otierney.net/objective-c.html.zh-tw.big5#downloading

9 34

Objective-C Beginner's Guide

08-9-1 8:56

@interface Cup: NSObject { int level; } -(int) level; -(void) setLevel: (int) l; -(void) fill; -(void) empty; -(void) print; @end

Cup.m#import #import #import #import #import "Cup.h" "CupOverflowException.h" "CupWarningException.h"

@implementation Cup -(id) init { self = [super init]; if ( self ) { [self setLevel: 0]; } return self; } -(int) level { return level; } -(void) setLevel: (int) l { level = l; if ( level > 100 ) { // throw overflow NSException *e = [CupOverflowException exceptionWithName: @"CupOverflowException" reason: @"The le