based on examples in "programming in objective-c," copyright © 2004 by sams publishing o...
TRANSCRIPT
![Page 1: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/1.jpg)
OBJECTIVE-C QUICK & DIRTY
![Page 2: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/2.jpg)
FILE EXTENSIONS
Header files
“.h file”
Implementation files
“.m file”
![Page 3: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/3.jpg)
CREATING CLASSESTHE INTERFACE
(FRACTION.H)#import <Foundation/NSObject.h>
@interface Fraction: NSObject {
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end
InheritanceClass:Parent
Instance variables
Instance methods
-= instance+ = class (static)
![Page 4: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/4.jpg)
CREATING CLASSESTHE IMPLEMENTATION
(FRACTION.M)#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
![Page 5: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/5.jpg)
THE CLIENT(MAIN.M)
#import <stdio.h>#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 itprintf( "The fraction is: " ); [frac print];printf( "\n" ); // free memory [frac release]; // if we alloc it, we need to release itreturn 0;
}
Output:
The fraction is: 1/3
![Page 6: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/6.jpg)
MULTIPLE PARAMETERSFUNKY SYNTAX
In fraction.h
... -(void) setNumerator: (int) n
andDenominator: (int) d; ...
![Page 7: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/7.jpg)
MULTIPLE PARAMETERSFUNKY SYNTAX
In fraction.m
... -(void) setNumerator: (int) n
andDenominator: (int) d {numerator = n; denominator = d;
} ...
![Page 8: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/8.jpg)
MULTIPLE PARAMETERSFUNKY SYNTAX – IN
MAIN.M#import <stdio.h> #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/3Fraction 2 is: 1/5
![Page 9: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/9.jpg)
CONSTRUCTORS
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
![Page 10: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/10.jpg)
CONSTRUCTORS
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 constructorif ( 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.mFraction *frac3 = [[Fraction alloc] initWithNumerator: 3
denominator: 10];
![Page 11: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/11.jpg)
ACCESS
@public, @private and @protected
Default is @protected
![Page 12: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/12.jpg)
ACCESS.H
#import <Foundation/NSObject.h>
@interface Access: NSObject {
@public
int publicVar;
@private
int privateVar; // private doesn’t go before each variable, unlike Java
int privateVar2;
@protected
int protectedVar;
}
@end
![Page 13: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/13.jpg)
ACCESS EXAMPLE – MAIN.M
#import "Access.h" #import <stdio.h> 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;
}
![Page 14: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/14.jpg)
OBJECT-ORIENTED FEATURES
Inheritance (not multiple)
Override parent class methods by putting implementation in the child class
In a Square.h file…
#import "Rectangle.h"
@interface Square: Rectangle // square inherits from rectangle
-(Square*) initWithSize: (int) s; // constructor
-(void) setSize: (int) s;
-(int) size;
@end
![Page 15: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/15.jpg)
BRACKETS VS. DOTS IS CONFUSING
Bracket Notation
[object method]
Or dot (.) notation
object.method
[frac setNumerator: 1] becomes frac.Numerator = 1
![Page 16: Based on examples in "Programming in Objective-C," Copyright © 2004 by Sams Publishing O BJECTIVE -C Q UICK & D IRTY](https://reader036.vdocuments.net/reader036/viewer/2022062714/56649d005503460f949d19ae/html5/thumbnails/16.jpg)
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