大纲
§ #import <stdio.h>
§
§ int main( int argc, const char *argv[] ) {
§ printf( "hello world/n" );
§ return 0;
}
hello world
§ #import <Foundation/NSObject.h>
§
§ @interface Fraction: NSObject {
§ int numerator;
§ int denominator;
§ }
§
§ -(void) print;
§ -(void) setNumerator: (int) d;
§ -(void) setDenominator: (int) d;
§ -(int) numerator;
§ -(int) denominator;
§ @end
§ #import "Fraction.h"
§ #import <stdio.h>
§
§ @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
§ #import <stdio.h>
§ #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;
}
The fraction is: 1/3
§ ...
§ -(void) setNumerator: (int) n andDenominator: (int) d;
...
§ ...
§ -(void) setNumerator: (int) n andDenominator: (int) d {
§ numerator = n;
§ denominator = d;
§ }
...
§ #import <stdio.h>
§ #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;
}
§ The fraction is: 1/3
Fraction 2 is: 1/5
§ ...
§ -(Fraction*) initWithNumerator: (int) n denominator: (int) d;
...
§ ...
§ -(Fraction*) initWithNumerator: (int) n denominator: (int) d {
§ self = [super init];
§
§ if ( self ) {
§ [self setNumerator: n andDenominator: d];
§ }
§
§ return self;
§ }
...
§ #import <stdio.h>
§ #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;
}
§ The fraction is: 1/3
§ Fraction 2 is: 1/5
Fraction 3 is: 3/10
§ #import <Foundation/NSObject.h>
§
§ @interface Access: NSObject {
§ @public
§ int publicVar;
§ @private
§ int privateVar;
§ int privateVar2;
§ @protected
§ int protectedVar;
§ }
@end
§ #import "Access.h"
§
§ @implementation Access
@end
§ #import "Access.h"
§ #import <stdio.h>
§
§ 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];
§ return 0;
}
public var: 5
§ #import <Foundation/NSObject.h>
§
§ static int count;
§
§ @interface ClassA: NSObject
§ +(int) initCount;
§ +(void) initialize;
@end
§ #import "ClassA.h"
§
§ @implementation ClassA
§ -(id) init {
§ self = [super init];
§ count++;
§ return self;
§ }
§
§ +(int) initCount {
§ return count;
§ }
§
§ +(void) initialize {
§ count = 0;
§ }
@end
§ #import "ClassA.h"
§ #import <stdio.h>
§
§ 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];
§ [c2 release];
§ [c3 release];
§
§ return 0;
}
§ ClassA count: 2
ClassA count: 3
§ #import <Foundation/NSException.h>
§
§ @interface CupWarningException: NSException
@end
§ #import "CupWarningException.h"
§
§ @implementation CupWarningException
@end
§ #import <Foundation/NSException.h>
§
§ @interface CupOverflowException: NSException
@end
§ #import "CupOverflowException.h"
§
§ @implementation CupOverflowException
@end
§ #import <Foundation/NSObject.h>
§
§ @interface Cup: NSObject {
§ int level;
§ }
§
§ -(int) level;
§ -(void) setLevel: (int) l;
§ -(void) fill;
§ -(void) empty;
§ -(void) print;
@end
§ #import "Cup.h"
§ #import "CupOverflowException.h"
§ #import "CupWarningException.h"
§ #import <Foundation/NSException.h>
§ #import <Foundation/NSString.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 level is above 100"
§ userInfo: nil];
§ @throw e;
§ } else if ( level >= 50 ) {
§ // throw warning
§ NSException *e = [CupWarningException
§ exceptionWithName: @"CupWarningException"
§ reason: @"The level is above or at 50"
§ userInfo: nil];
§ @throw e;
§ } else if ( level < 0 ) {
§ // throw exception
§ NSException *e = [NSException
§ exceptionWithName: @"CupUnderflowException"
§ reason: @"The level is below 0"
§ userInfo: nil];
§ @throw e;
§ }
§ }
§
§ -(void) fill {
§ [self setLevel: level + 10];
§ }
§
§ -(void) empty {
§ [self setLevel: level - 10];
§ }
§
§ -(void) print {
§ printf( "Cup level is: %i/n", level );
§ }
@end
§ #import "Cup.h"
§ #import "CupOverflowException.h"
§ #import "CupWarningException.h"
§ #import <Foundation/NSString.h>
§ #import <Foundation/NSException.h>
§ #import <Foundation/NSAutoreleasePool.h>
§ #import <stdio.h>
§
§ int main( int argc, const char *argv[] ) {
§ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
§ Cup *cup = [[Cup alloc] init];
§ int i;
§
§ // this will work
§ for ( i = 0; i < 4; i++ ) {
§ [cup fill];
§ [cup print];
§ }
§
§ // this will throw exceptions
§ for ( i = 0; i < 7; i++ ) {
§ @try {
§ [cup fill];
§ } @catch ( CupWarningException *e ) {
§ printf( "%s: ", [[e name] cString] );
§ } @catch ( CupOverflowException *e ) {
§ printf( "%s: ", [[e name] cString] );
§ } @finally {
§ [cup print];
§ }
§ }
§
§ // throw a generic exception
§ @try {
§ [cup setLevel: -1];
§ } @catch ( NSException *e ) {
§ printf( "%s: %s/n", [[e name] cString], [[e reason] cString] );
§ }
§
§ // free memory
§ [cup release];
§ [pool release];
}
§ Cup level is: 10
§ Cup level is: 20
§ Cup level is: 30
§ Cup level is: 40
§ CupWarningException: Cup level is: 50
§ CupWarningException: Cup level is: 60
§ CupWarningException: Cup level is: 70
§ CupWarningException: Cup level is: 80
§ CupWarningException: Cup level is: 90
§ CupWarningException: Cup level is: 100
§ CupOverflowException: Cup level is: 110
CupUnderflowException: The level is below 0
§ #import <Foundation/NSObject.h>
§
§ @interface Fraction: NSObject {
§ int numerator;
§ int denominator;
§ }
§
§ -(Fraction*) initWithNumerator: (int) n denominator: (int) d;
§ -(void) print;
§ -(void) setNumerator: (int) d;
§ -(void) setDenominator: (int) d;
§ -(void) setNumerator: (int) n andDenominator: (int) d;
§ -(int) numerator;
§ -(int) denominator;
@end
§ #import "Fraction.h"
§ #import <stdio.h>
§
§ @implementation Fraction
§ -(Fraction*) initWithNumerator: (int) n denominator: (int) d {
§ self = [super init];
§
§ if ( self ) {
§ [self setNumerator: n andDenominator: d];
§ }
§
§ return self;
§ }
§
§ -(void) print {
§ printf( "%i / %i", numerator, denominator );
§ }
§
§ -(void) setNumerator: (int) n {
§ numerator = n;
§ }
§
§ -(void) setDenominator: (int) d {
§ denominator = d;
§ }
§
§ -(void) setNumerator: (int) n andDenominator: (int) d {
§ numerator = n;
§ denominator = d;
§ }
§
§ -(int) denominator {
§ return denominator;
§ }
§
§ -(int) numerator {
§ return numerator;
§ }
@end
§ #import <Foundation/NSObject.h>
§
§ @interface Complex: NSObject {
§ double real;
§ double imaginary;
§ }
§
§ -(Complex*) initWithReal: (double) r andImaginary: (double) i;
§ -(void) setReal: (double) r;
§ -(void) setImaginary: (double) i;
§ -(void) setReal: (double) r andImaginary: (double) i;
§ -(double) real;
§ -(double) imaginary;
§ -(void) print;
§
@end
§ #import "Complex.h"
§ #import <stdio.h>
§
§ @implementation Complex
§ -(Complex*) initWithReal: (double) r andImaginary: (double) i {
§ self = [super init];
§
§ if ( self ) {
§ [self setReal: r andImaginary: i];
§ }
§
§ return self;
§ }
§
§ -(void) setReal: (double) r {
§ real = r;
§ }
§
§ -(void) setImaginary: (double) i {
§ imaginary = i;
§ }
§
§ -(void) setReal: (double) r andImaginary: (double) i {
§ real = r;
§ imaginary = i;
§ }
§
§ -(double) real {
§ return real;
§ }
§
§ -(double) imaginary {
§ return imaginary;
§ }
§
§ -(void) print {
§ printf( "%_f + %_fi", real, imaginary );
§ }
§
@end
§ #import <stdio.h>
§ #import "Fraction.h"
§ #import "Complex.h"
§
§ int main( int argc, const char *argv[] ) {
§ // create a new instance
§ Fraction *frac = [[Fraction alloc] initWithNumerator: 1 denominator: 10];
§ Complex *comp = [[Complex alloc] initWithReal: 10 andImaginary: 15];
§ id number;
§
§ // print fraction
§ number = frac;
§ printf( "The fraction is: " );
§ [number print];
§ printf( "/n" );
§
§ // print complex
§ number = comp;
§ printf( "The complex number is: " );
§ [number print];
§ printf( "/n" );
§
§ // free memory
§ [frac release];
§ [comp release];
§
§ return 0;
}
§ The fraction is: 1 / 10
The complex number is: 10.000000 + 15.000000i
§ #import <Foundation/NSObject.h>
§
§ @interface Rectangle: NSObject {
§ int width;
§ int height;
§ }
§
§ -(Rectangle*) initWithWidth: (int) w height: (int) h;
§ -(void) setWidth: (int) w;
§ -(void) setHeight: (int) h;
§ -(void) setWidth: (int) w height: (int) h;
§ -(int) width;
§ -(int) height;
§ -(void) print;
@end
§ #import "Rectangle.h"
§ #import <stdio.h>
§
§ @implementation Rectangle
§ -(Rectangle*) initWithWidth: (int) w height: (int) h {
§ self = [super init];
§
§ if ( self ) {
§ [self setWidth: w height: h];
§ }
§
§ return self;
§ }
§
§ -(void) setWidth: (int) w {
§ width = w;
§ }
§
§ -(void) setHeight: (int) h {
§ height = h;
§ }
§
§ -(void) setWidth: (int) w height: (int) h {
§ width = w;
§ height = h;
§ }
§
§ -(int) width {
§ return width;
§ }
§
§ -(int) height {
§ return height;
§ }
§
§ -(void) print {
§ printf( "width = %i, height = %i", width, height );
§ }
@end
§ #import "Rectangle.h"
§
§ @interface Square: Rectangle
§ -(Square*) initWithSize: (int) s;
§ -(void) setSize: (int) s;
§ -(int) size;
@end
§ #import "Square.h"
§
§ @implementation Square
§ -(Square*) initWithSize: (int) s {
§ self = [super init];
§
§ if ( self ) {
§ [self setSize: s];
§ }
§
§ return self;
§ }
§
§ -(void) setSize: (int) s {
§ width = s;
§ height = s;
§ }
§
§ -(int) size {
§ return width;
§ }
§
§ -(void) setWidth: (int) w {
§ [self setSize: w];
§ }
§
§ -(void) setHeight: (int) h {
§ [self setSize: h];
§ }
@end
§ #import "Square.h"
§ #import "Rectangle.h"
§ #import <stdio.h>
§
§ int main( int argc, const char *argv[] ) {
§ Rectangle *rec = [[Rectangle alloc] initWithWidth: 10 height: 20];
§ Square *sq = [[Square alloc] initWithSize: 15];
§
§ // print em
§ printf( "Rectangle: " );
§ [rec print];
§ printf( "/n" );
§
§ printf( "Square: " );
§ [sq print];
§ printf( "/n" );
§
§ // update square
§ [sq setWidth: 20];
§ printf( "Square after change: " );
§ [sq print];
§ printf( "/n" );
§
§ // free memory
§ [rec release];
§ [sq release];
§
§ return 0;
}
§ Rectangle: width = 10, height = 20
§ Square: width = 15, height = 15
Square after change: width = 20, height = 20
-(BOOL) isKindOfClass: classObj |
is object a descendent or member of classObj |
-(BOOL) isMemberOfClass: classObj |
is object a member of classObj |
-(BOOL) respondsToSelector: selector |
does the object have a method named specifiec by the selector |
+(BOOL) instancesRespondToSelector: selector |
does an object created by this class have the ability to respond to the specified selector |
-(id) performSelector: selector |
invoke the specified selector on the object |
§ #import "Square.h"
§ #import "Rectangle.h"
§ #import <stdio.h>
§
§ int main( int argc, const char *argv[] ) {
§ Rectangle *rec = [[Rectangle alloc] initWithWidth: 10 height: 20];
§ Square *sq = [[Square alloc] initWithSize: 15];
§
§ // isMemberOfClass
§
§ // true
§ if ( [sq isMemberOfClass: [Square class]] == YES ) {
§ printf( "square is a member of square class/n" );
§ }
§
§ // false
§ if ( [sq isMemberOfClass: [Rectangle class]] == YES ) {
§ printf( "square is a member of rectangle class/n" );
§ }
§
§ // false
§ if ( [sq isMemberOfClass: [NSObject class]] == YES ) {
§ printf( "square is a member of object class/n" );
§ }
§
§ // isKindOfClass
§
§ // true
§ if ( [sq isKindOfClass: [Square class]] == YES ) {
§ printf( "square is a kind of square class/n" );
§ }
§
§ // true
§ if ( [sq isKindOfClass: [Rectangle class]] == YES ) {
§ printf( "square is a kind of rectangle class/n" );
§ }
§
§ // true
§ if ( [sq isKindOfClass: [NSObject class]] == YES ) {
§ printf( "square is a kind of object class/n" );
§ }
§
§ // respondsToSelector
§
§ // true
§ if ( [sq respondsToSelector: @selector( setSize: )] == YES ) {
§ printf( "square responds to setSize: method/n" );
§ }
§
§ // false
§ if ( [sq respondsToSelector: @selector( nonExistant )] == YES ) {
§ printf( "square responds to nonExistant method/n" );
§ }
§
§ // true
§ if ( [Square respondsToSelector: @selector( alloc )] == YES ) {
§ printf( "square class responds to alloc method/n" );
§ }
§
§ // instancesRespondToSelector
§
§ // false
§ if ( [Rectangle instancesRespondToSelector: @selector( setSize: )] == YES ) {
§ printf( "rectangle instance responds to setSize: method/n" );
§ }
§
§ // true
§ if ( [Square instancesRespondToSelector: @selector( setSize: )] == YES ) {
§ printf( "square instance responds to setSize: method/n" );
§ }
§
§ // free memory
§ [rec release];
§ [sq release];
§
§ return 0;
}
§ square is a member of square class
§ square is a kind of square class
§ square is a kind of rectangle class
§ square is a kind of object class
§ square responds to setSize: method
§ square class responds to alloc method
square instance responds to setSize: method
§ #import "Fraction.h"
§
§ @interface Fraction (Math)
§ -(Fraction*) add: (Fraction*) f;
§ -(Fraction*) mul: (Fraction*) f;
§ -(Fraction*) div: (Fraction*) f;
§ -(Fraction*) sub: (Fraction*) f;
@end
§ #import "FractionMath.h"
§
§ @implementation Fraction (Math)
§ -(Fraction*) add: (Fraction*) f {
§ return [[Fraction alloc] initWithNumerator: numerator * [f denominator] +
§ denominator * [f numerator]
§ denominator: denominator * [f denominator]];
§ }
§
§ -(Fraction*) mul: (Fraction*) f {
§ return [[Fraction alloc] initWithNumerator: numerator * [f numerator]
§ denominator: denominator * [f denominator]];
§
§ }
§
§ -(Fraction*) div: (Fraction*) f {
§ return [[Fraction alloc] initWithNumerator: numerator * [f denominator]
§ denominator: denominator * [f numerator]];
§ }
§
§ -(Fraction*) sub: (Fraction*) f {
§ return [[Fraction alloc] initWithNumerator: numerator * [f denominator] -
§ denominator * [f numerator]
§ denominator: denominator * [f denominator]];
§ }
@end
§ #import <stdio.h>
§ #import "Fraction.h"
§ #import "FractionMath.h"
§
§ int main( int argc, const char *argv[] ) {
§ // create a new instance
§ Fraction *frac1 = [[Fraction alloc] initWithNumerator: 1 denominator: 3];
§ Fraction *frac2 = [[Fraction alloc] initWithNumerator: 2 denominator: 5];
§ Fraction *frac3 = [frac1 mul: frac2];
§
§ // print it
§ [frac1 print];
§ printf( " * " );
§ [frac2 print];
§ printf( " = " );
§ [frac3 print];
§ printf( "/n" );
§
§ // free memory
§ [frac1 release];
§ [frac2 release];
§ [frac3 release];
§
§ return 0;
}
1/3 * 2/5 = 2/15
§ #import <Foundation/NSObject.h>
§
§ @interface MyClass: NSObject
§ -(void) publicMethod;
@end
§ #import "MyClass.h"
§ #import <stdio.h>
§
§ @implementation MyClass
§ -(void) publicMethod {
§ printf( "public method/n" );
§ }
§ @end
§
§ // private methods
§ @interface MyClass (Private)
§ -(void) privateMethod;
§ @end
§
§ @implementation MyClass (Private)
§ -(void) privateMethod {
§ printf( "private method/n" );
§ }
@end
§ #import "MyClass.h"
§
§ int main( int argc, const char *argv[] ) {
§ MyClass *obj = [[MyClass alloc] init];
§
§ // this compiles
§ [obj publicMethod];
§
§ // this throws errors when compiling
§ //[obj privateMethod];
§
§ // free memory
§ [obj release];
§
§ return 0;
}
public method
§ #import "Fraction.h"
§
§ @interface FractionB: Fraction
§ -(void) print;
§ @end
§ #import "FractionB.h"
§ #import <stdio.h>
§
§ @implementation FractionB
§ -(void) print {
§ printf( "(%i/%i)", numerator, denominator );
§ }
@end
§ #import <stdio.h>
§ #import "Fraction.h"
§ #import "FractionB.h"
§
§ int main( int argc, const char *argv[] ) {
§ Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
§
§ // print it
§ printf( "The fraction is: " );
§ [frac print];
§ printf( "/n" );
§
§ // make FractionB pose as Fraction
§ [FractionB poseAsClass: [Fraction class]];
§
§ Fraction *frac2 = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
§
§ // print it
§ printf( "The fraction is: " );
§ [frac2 print];
§ printf( "/n" );
§
§ // free memory
§ [frac release];
§ [frac2 release];
§
§ return 0;
}
§ The fraction is: 3/10
The fraction is: (3/10)
§ @protocol Printing
§ -(void) print;
@end
§ #import <Foundation/NSObject.h>
§ #import "Printing.h"
§
§ @interface Fraction: NSObject <Printing, NSCopying> {
§ int numerator;
§ int denominator;
§ }
§
§ -(Fraction*) initWithNumerator: (int) n denominator: (int) d;
§ -(void) setNumerator: (int) d;
§ -(void) setDenominator: (int) d;
§ -(void) setNumerator: (int) n andDenominator: (int) d;
§ -(int) numerator;
§ -(int) denominator;
@end
§ #import "Fraction.h"
§ #import <stdio.h>
§
§ @implementation Fraction
§ -(Fraction*) initWithNumerator: (int) n denominator: (int) d {
§ self = [super init];
§
§ if ( self ) {
§ [self setNumerator: n andDenominator: d];
§ }
§
§ return self;
§ }
§
§ -(void) print {
§ printf( "%i/%i", numerator, denominator );
§ }
§
§ -(void) setNumerator: (int) n {
§ numerator = n;
§ }
§
§ -(void) setDenominator: (int) d {
§ denominator = d;
§ }
§
§ -(void) setNumerator: (int) n andDenominator: (int) d {
§ numerator = n;
§ denominator = d;
§ }
§
§ -(int) denominator {
§ return denominator;
§ }
§
§ -(int) numerator {
§ return numerator;
§ }
§
§ -(Fraction*) copyWithZone: (NSZone*) zone {
§ return [[Fraction allocWithZone: zone] initWithNumerator: numerator
§ denominator: denominator];
§ }
@end
§ #import <Foundation/NSObject.h>
§ #import "Printing.h"
§
§ @interface Complex: NSObject <Printing> {
§ double real;
§ double imaginary;
§ }
§
§ -(Complex*) initWithReal: (double) r andImaginary: (double) i;
§ -(void) setReal: (double) r;
§ -(void) setImaginary: (double) i;
§ -(void) setReal: (double) r andImaginary: (double) i;
§ -(double) real;
§ -(double) imaginary;
@end
§ #import "Complex.h"
§ #import <stdio.h>
§
§ @implementation Complex
§ -(Complex*) initWithReal: (double) r andImaginary: (double) i {
§ self = [super init];
§
§ if ( self ) {
§ [self setReal: r andImaginary: i];
§ }
§
§ return self;
§ }
§
§ -(void) setReal: (double) r {
§ real = r;
§ }
§
§ -(void) setImaginary: (double) i {
§ imaginary = i;
§ }
§
§ -(void) setReal: (double) r andImaginary: (double) i {
§ real = r;
§ imaginary = i;
§ }
§
§ -(double) real {
§ return real;
§ }
§
§ -(double) imaginary {
§ return imaginary;
§ }
§
§ -(void) print {
§ printf( "%_f + %_fi", real, imaginary );
§ }
@end
§ #import <stdio.h>
§ #import "Fraction.h"
§ #import "Complex.h"
§
§ int main( int argc, const char *argv[] ) {
§ // create a new instance
§ Fraction *frac = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
§ Complex *comp = [[Complex alloc] initWithReal: 5 andImaginary: 15];
§ id <Printing> printable;
§ id <NSCopying, Printing> copyPrintable;
§
§ // print it
§ printable = frac;
§ printf( "The fraction is: " );
§ [printable print];
§ printf( "/n" );
§
§ // print complex
§ printable = comp;
§ printf( "The complex number is: " );
§ [printable print];
§ printf( "/n" );
§
§ // this compiles because Fraction comforms to both Printing and NSCopyable
§ copyPrintable = frac;
§
§ // this doesn't compile because Complex only conforms to Printing
§ //copyPrintable = comp;
§
§ // test conformance
§
§ // true
§ if ( [frac conformsToProtocol: @protocol( NSCopying )] == YES ) {
§ printf( "Fraction conforms to NSCopying/n" );
§ }
§
§ // false
§ if ( [comp conformsToProtocol: @protocol( NSCopying )] == YES ) {
§ printf( "Complex conforms to NSCopying/n" );
§ }
§
§ // free memory
§ [frac release];
§ [comp release];
§
§ return 0;
}
§ The fraction is: 3/10
§ The complex number is: 5.000000 + 15.000000i
Fraction conforms to NSCopying
§ ...
§ -(void) dealloc {
§ printf( "Deallocing fraction/n" );
§ [super dealloc];
§ }
...
§ #import "Fraction.h"
§ #import <stdio.h>
§
§ int main( int argc, const char *argv[] ) {
§ Fraction *frac1 = [[Fraction alloc] init];
§ Fraction *frac2 = [[Fraction alloc] init];
§
§ // print current counts
§ printf( "Fraction 1 retain count: %i/n", [frac1 retainCount] );
§ printf( "Fraction 2 retain count: %i/n", [frac2 retainCount] );
§
§ // increment them
§ [frac1 retain]; // 2
§ [frac1 retain]; // 3
§ [frac2 retain]; // 2
§
§ // print current counts
§ printf( "Fraction 1 retain count: %i/n", [frac1 retainCount] );
§ printf( "Fraction 2 retain count: %i/n", [frac2 retainCount] );
§
§ // decrement
§ [frac1 release]; // 2
§ [frac2 release]; // 1
§
§ // print current counts
§ printf( "Fraction 1 retain count: %i/n", [frac1 retainCount] );
§ printf( "Fraction 2 retain count: %i/n", [frac2 retainCount] );
§
§ // release them until they dealloc themselves
§ [frac1 release]; // 1
§ [frac1 release]; // 0
§ [frac2 release]; // 0
}
§ Fraction 1 retain count: 1
§ Fraction 2 retain count: 1
§ Fraction 1 retain count: 3
§ Fraction 2 retain count: 2
§ Fraction 1 retain count: 2
§ Fraction 2 retain count: 1
§ Deallocing fraction
Deallocing fraction
§ #import <Foundation/NSObject.h>
§ #import <Foundation/NSString.h>
§
§ @interface AddressCard: NSObject {
§ NSString *first;
§ NSString *last;
§ NSString *email;
§ }
§
§ -(AddressCard*) initWithFirst: (NSString*) f
§ last: (NSString*) l
§ email: (NSString*) e;
§ -(NSString*) first;
§ -(NSString*) last;
§ -(NSString*) email;
§ -(void) setFirst: (NSString*) f;
§ -(void) setLast: (NSString*) l;
§ -(void) setEmail: (NSString*) e;
§ -(void) setFirst: (NSString*) f
§ last: (NSString*) l
§ email: (NSString*) e;
§ -(void) setFirst: (NSString*) f last: (NSString*) l;
§ -(void) print;
@end
§ #import "AddressCard.h"
§ #import <stdio.h>
§
§ @implementation AddressCard
§ -(AddressCard*) initWithFirst: (NSString*) f
§ last: (NSString*) l
§ email: (NSString*) e {
§ self = [super init];
§
§ if ( self ) {
§ [self setFirst: f last: l email: e];
§ }
§
§ return self;
§ }
§
§ -(NSString*) first {
§ return first;
§ }
§
§ -(NSString*) last {
§ return last;
§ }
§
§ -(NSString*) email {
§ return email;
§ }
§
§ -(void) setFirst: (NSString*) f {
§ [f retain];
§ [first release];
§ first = f;
§ }
§
§ -(void) setLast: (NSString*) l {
§ [l retain];
§ [last release];
§ last = l;
§ }
§
§ -(void) setEmail: (NSString*) e {
§ [e retain];
§ [email release];
§ email = e;
§ }
§
§ -(void) setFirst: (NSString*) f
§ last: (NSString*) l
§ email: (NSString*) e {
§ [self setFirst: f];
§ [self setLast: l];
§ [self setEmail: e];
§ }
§
§ -(void) setFirst: (NSString*) f last: (NSString*) l {
§ [self setFirst: f];
§ [self setLast: l];
§ }
§
§ -(void) print {
§ printf( "%s %s <%s>", [first cString],
§ [last cString],
§ [email cString] );
§ }
§
§ -(void) dealloc {
§ [first release];
§ [last release];
§ [email release];
§
§ [super dealloc];
§ }
@end
§ #import "AddressCard.h"
§ #import <Foundation/NSString.h>
§ #import <stdio.h>
§
§ int main( int argc, const char *argv[] ) {
§ NSString *first =[[NSString alloc] initWithCString: "Tom"];
§ NSString *last = [[NSString alloc] initWithCString: "Jones"];
§ NSString *email = [[NSString alloc] initWithCString: "[email protected]"];
§ AddressCard *tom = [[AddressCard alloc] initWithFirst: first
§ last: last
§ email: email];
§
§ // we're done with the strings, so we must dealloc them
§ [first release];
§ [last release];
§ [email release];
§
§ // print to show the retain count
§ printf( "Retain count: %i/n", [[tom first] retainCount] );
§ [tom print];
§ printf( "/n" );
§
§ // free memory
§ [tom release];
§
§ return 0;
}
§ Retain count: 1
Tom Jones <[email protected]>
§ #import <Foundation/NSString.h>
§ #import <Foundation/NSAutoreleasePool.h>
§ #import <stdio.h>
§
§ int main( int argc, const char *argv[] ) {
§ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
§ NSString *str1 = @"constant string";
§ NSString *str2 = [NSString stringWithString: @"string managed by the pool"];
§ NSString *str3 = [[NSString alloc] initWithString: @"self managed string"];
§
§ // print the strings
§ printf( "%s retain count: %x/n", [str1 cString], [str1 retainCount] );
§ printf( "%s retain count: %x/n", [str2 cString], [str2 retainCount] );
§ printf( "%s retain count: %x/n", [str3 cString], [str3 retainCount] );
§
§ // free memory
§ [str3 release];
§
§ // free pool
§ [pool release];
§ return 0;
}
§ constant string retain count: ffffffff
§ string managed by the pool retain count: 1
self managed string retain count: 1
§ ...
§ +(Fraction*) fractionWithNumerator: (int) n denominator: (int) d;
§ ...
§ ...
§ +(Fraction*) fractionWithNumerator: (int) n denominator: (int) d {
§ Fraction *ret = [[Fraction alloc] initWithNumerator: n denominator: d];
§ [ret autorelease];
§
§ return ret;
§ }
...
§ #import <Foundation/NSAutoreleasePool.h>
§ #import "Fraction.h"
§ #import <stdio.h>
§
§ int main( int argc, const char *argv[] ) {
§ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
§ Fraction *frac1 = [Fraction fractionWithNumerator: 2 denominator: 5];
§ Fraction *frac2 = [Fraction fractionWithNumerator: 1 denominator: 3];
§
§ // print frac 1
§ printf( "Fraction 1: " );
§ [frac1 print];
§ printf( "/n" );
§
§ // print frac 2
§ printf( "Fraction 2: " );
§ [frac2 print];
§ printf( "/n" );
§
§ // this causes a segmentation fault
§ //[frac1 release];
§
§ // release the pool and all objects in it
§ [pool release];
§ return 0;
}
§ Fraction 1: 2/5
Fraction 2: 1/3
§ #import <Foundation/NSArray.h>
§ #import <Foundation/NSString.h>
§ #import <Foundation/NSAutoreleasePool.h>
§ #import <Foundation/NSEnumerator.h>
§ #import <stdio.h>
§
§ void print( NSArray *array ) {
§ NSEnumerator *enumerator = [array objectEnumerator];
§ id obj;
§
§ while ( obj = [enumerator nextObject] ) {
§ printf( "%s/n", [[obj description] cString] );
§ }
§ }
§
§ int main( int argc, const char *argv[] ) {
§ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
§ NSArray *arr = [[NSArray alloc] initWithObjects:
§ @"Me", @"Myself", @"I", nil];
§ NSMutableArray *mutable = [[NSMutableArray alloc] init];
§
§ // enumerate over items
§ printf( "----static array/n" );
§ print( arr );
§
§ // add stuff
§ [mutable addObject: @"One"];
§ [mutable addObject: @"Two"];
§ [mutable addObjectsFromArray: arr];
§ [mutable addObject: @"Three"];
§
§ // print em
§ printf( "----mutable array/n" );
§ print( mutable );
§
§ // sort then print
§ printf( "----sorted mutable array/n" );
§ [mutable sortUsingSelector: @selector( caseInsensitiveCompare: )];
§ print( mutable );
§
§ // free memory
§ [arr release];
§ [mutable release];
§ [pool release];
§
§ return 0;
}
§ ----static array
§ Me
§ Myself
§ I
§ ----mutable array
§ One
§ Two
§ Me
§ Myself
§ I
§ Three
§ ----sorted mutable array
§ I
§ Me
§ Myself
§ One
§ Three
Two
§ #import <Foundation/NSString.h>
§ #import <Foundation/NSAutoreleasePool.h>
§ #import <Foundation/NSDictionary.h>
§ #import <Foundation/NSEnumerator.h>
§ #import <Foundation/Foundation.h<
§ #import <stdio.h>
§
§ void print( NSDictionary *map ) {
§ NSEnumerator *enumerator = [map keyEnumerator];
§ id key;
§
§ while ( key = [enumerator nextObject] ) {
§ printf( "%s => %s/n",
§ [[key description] cString],
§ [[[map objectForKey: key] description] cString] );
§ }
§ }
§
§ int main( int argc, const char *argv[] ) {
§ NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
§ NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:
§ @"one", [NSNumber numberWithInt: 1],
§ @"two", [NSNumber numberWithInt: 2],
§ @"three", [NSNumber numberWithInt: 3],
§ nil];
§ NSMutableDictionary *mutable = [[NSMutableDictionary alloc] init];
§
§ // print dictionary
§ printf( "----static dictionary/n" );
§ print( dictionary );
§
§ // add objects
§ [mutable setObject: @"Tom" forKey: @"[email protected]"];
§ [mutable setObject: @"Bob" forKey: @"[email protected]" ];
§
§ // print mutable dictionary
§ printf( "----mutable dictionary/n" );
§ print( mutable );
§
§ // free memory
§ [dictionary release];
§ [mutable release];
§ [pool release];
§
§ return 0;
}
§ ----static dictionary
§ 1 => one
§ 2 => two
§ 3 => three
§ ----mutable dictionary
§ [email protected] => Bob
[email protected] => Tom
Last modified: April 13, 2004.
中文翻译:William Shih (xamous),January 7, 2005