Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY
F ILE E XTENSIONS Header files “.h file” Implementation files “.m file”
C REATING C LASSES T HE INTERFACE (F RACTION. H ) Fraction: NSObject { int numerator; int denominator; } -(void) print; -(void) setNumerator: (int) n; -(void) setDenominator: (int) d; -(int) numerator; -(int) Inheritance Class:Parent Instance variables Instance methods -= instance + = class (static)
C REATING C LASSES T HE IMPLEMENTATION (F RACTION. M ) #import "Fraction.h“ 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;
T HE C LIENT (M AIN. M ) #import #import "Fraction.h" int main( int argc, const char *argv[] ) { Fraction *frac = [[Fraction alloc] init]; // create a new instance // alloc gets the memory and init is the constructor // set the values [frac setNumerator: 1]; // calling setNumerator on frac and passing 1 [frac setDenominator: 3]; // print it printf( "The fraction is: " ); [frac print]; printf( "\n" ); // free memory [frac release];// if we alloc it, we need to release it return 0; } Output: The fraction is: 1/3
M ULTIPLE P ARAMETERS F UNKY S YNTAX In fraction.h... -(void) setNumerator: (int) n andDenominator: (int) d;...
M ULTIPLE P ARAMETERS F UNKY S YNTAX In fraction.m... -(void) setNumerator: (int) n andDenominator: (int) d { numerator = n; denominator = d; }...
M ULTIPLE P ARAMETERS F UNKY S YNTAX – I N M AIN. M #import #import "Fraction.h" int main( int argc, const char *argv[] ) { Fraction *frac = [[Fraction alloc] init]; // create new instances Fraction *frac2 = [[Fraction alloc] init]; [frac setNumerator: 1]; // set the values [frac setDenominator: 3]; [frac2 setNumerator: 1 andDenominator: 5]; // combined set printf( "The fraction is: " ); [frac print]; printf( "\n" ); printf( "Fraction 2 is: " ); [frac2 print]; printf( "\n" ); [frac release]; // free memory [frac2 release]; return 0; } Output: The fraction is: 1/3 Fraction 2 is: 1/5
C ONSTRUCTORS They are just a method named init Not a special construct like in Java or C++ Default constructor -(id) init; Id is a generic type for an arbitrary object
C ONSTRUCTORS In Fraction.h... -(Fraction*) initWithNumerator: (int) n denominator: (int) d;... In Fraction.m... -(Fraction*) initWithNumerator: (int) n denominator: (int) d { self = [super init]; // call the base class constructor if ( self ) { // if (self != nil) – making sure we didn’t run out of memory on last call [self setNumerator: n andDenominator: d]; } return self; // I’m a constructor }... In main.m Fraction *frac3 = [[Fraction alloc] initWithNumerator: 3 denominator: 10];
A Default
A CCESS. H Access: NSObject int int privateVar; // private doesn’t go before each variable, unlike Java int int protectedVar;
A CCESS E XAMPLE – M AIN. M #import "Access.h" #import int main( int argc, const char *argv[] ) { Access *a = [[Access alloc] init]; // works a->publicVar = 5; // notice the -> notation here. printf( "public var: %i\n", a->publicVar ); // doesn't compile //a->privateVar = 10; //printf( "private var: %i\n", a->privateVar ); [a release]; return 0; }
O BJECT -O RIENTED F EATURES Inheritance (not multiple) Override parent class methods by putting implementation in the child class In a Square.h file… #import Square: Rectangle // square inherits from rectangle -(Square*) initWithSize: (int) s; // constructor -(void) setSize: (int) s; -(int)
B RACKETS VS. D OTS IS C ONFUSING Bracket Notation [object method] Or dot (.) notation object.method [frac setNumerator: 1] becomes frac.Numerator = 1
MISC The symbol is used to introduce Objective-C keywords so they won’t conflict with the C or C++ stuff You will see a lot of “NS” – “NextStep” – code from back in the day. YES and NO instead of TRUE and FALSE