CS 696 Mobile Application DevelopmentFall Semester, 2010Doc 3 Objective C - Class BasicsSep 2, 2010Copyright ©, All rights reserved. 2010 SDSU & Roger Whitney, 5500 Campanile Drive, San Diego, CA 92182-7700 USA. OpenContent (http://www.opencontent.org/openpub/) license defines the copyright on this document.References2The Objective-C 2.0 Programming Language, http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html#//apple_ref/doc/uid/TP30001163Classes & Instance3Class Template for objectsDefines methods and instance variables for objectsDefining a Class4@interface Rectangle : NSObject{ int width; int height;}- (void) setWidth: (int) newWidth;- (int) width;- (void) setHeight: (int) newHeight;- (int) height;- (int) area;@endRectangle.h#import "Rectangle.h"@implementation Rectangle- (void) setWidth:(int)newWidth { width = newWidth;}- (int) width { return width;}- (void) setHeight:(int)newHeight { height = newHeight;}- (int) height {return height; }- (int) area { return width * height; }@endRectangle.mUsing the Class5#import "Rectangle.h"int main (int argc, const char * argv[]) { Rectangle * sample = [[Rectangle alloc] init]; int initialValue = [sample width]; NSLog(@"initialValue: %i", initialValue); [sample setWidth:4]; [sample setHeight:5]; int area = [sample area]; NSLog(@"area %i", area); [sample release]; return 0;}One can use printf("area %i", area) if you add "#import <stdio.h>" NSLog provides a bit more information and is the standard in Objective-CHeader File Format6#import Imports here@interface ClassName : ParentClass{ // Declare instance variables} // Declare instance and class methods- (returnType) instanceMethod;+ (returnType) classMethod;@endTopics7NewNSObject Multiple argumentsself, superInstance and Class methodsOverloadingOverridingAccess levelsConstructorsConvenience constructorsDestructorsDescriptionnew & alloc - init8Personal style on which to useMost references use alloc & init[Rectangle new] same as [[Rectangle alloc] init]NSObject9Single root of class inheritance treeAncestor class of all Objective C classes@interface Rectangle : NSObject{ int width; int height;}No implicit parent10@interface Rectangle{ int width; int height;}This rectangle class does not have a parent class. It does not inherit from NSObject and basically will not workWhat is with the NS prefix?11No namespacesUse package abbreviation as prefix for class namesNS - NextStepMultiple Arguments12@interface Rectangle : NSObject{ int width; int height;}- (void) setHeight: (int) newHeight width: (int) newWidth;@endRectangle.h#import "Rectangle.h"@implementation Rectangle- (void) setHeight: (int) newHeight width: (int) newWidth { height = newHeight; width = newWidth;}@endRectangle.mself & super13self - same as Java's thissuper - same as Java's super@implementation Rectangle- (int) width { return width; }- (int) height { return height; }- (int) area { return [self width] * [self height];}@endExample14ParentChildGrandChild- (void) foo{ NSLog(@"Parent foo");}- (void) foo{ NSLog(@"Child foo");}- (void) foo{ NSLog(@"GrandChild foo");}- (void) testSuper{ NSLog(@"child testSuper"); [super foo];}- (void) testSelf{ NSLog(@"child testSelf"); [self foo];}Child Methods Parent * example = [GrandChild new]; [example testSelf]; [example testSuper];Instance & Class Methods15@interface ClassInstanceExample : NSObject {}- (void) instanceMethod;- (void) testMethod;+ (void) classMethod;@end#import "ClassInstanceExample.h"@implementation ClassInstanceExample- (void) instanceMethod { NSLog(@"instance");}- (void) testMethod { [[self class] classMethod];}+ (void) classMethod { NSLog(@"class");}@endCalling the methods16 [ClassInstanceExample classMethod]; ClassInstanceExample * instance = [ClassInstanceExample new]; [instance instanceMethod]; [instance testMethod];Static Variables17@interface Counter : NSObject{ int count;}- (int) instanceNext;- (int) instanceGlobalAccess;+ (int) classNext;@end#import "Counter.h"static int gCount = 0;@implementation Counter-(int) instanceNext { return count++;}- (int) instanceGlobalAccess { return gCount++;}+ (int) classNext { return gCount++;}@endNaming convention: start global variables with "g"Static variables act like private class variables.Initial Values18Instance variables & Globalsinitialized to 0 or nullExcept unionsLocal variablesnot initializedNo Overloading methods19@interface OverLoadExample : NSObject {}- (void) method: (int) cat;- (void) method: (NSString *) dog; //Compile error@endOverriding Methods20@interface Parent : NSObject {}- (void) hello;+ (void) bye;@end#import "Parent.h"@implementation Parent- (void) hello { NSLog(@"Parent Hello");}+ (void) bye { NSLog(@"Parent bye");}@endChild Class21#import "Parent.h"@interface Child : Parent { }@end#import "Child.h"@implementation Child- (void) hello { [super hello]; NSLog(@"Child Hello");}+ (void) bye { [super bye]; NSLog(@"Child bye");}@endExample22 [Parent bye]; //Parent bye [Child bye]; //Parent bye \n Child bye Parent * example = [Child new]; [example hello]; //Parent Hello \n Child helloInstance Variable Access Levels23@interface Sample : NSObject{ int x; int y;@private int a; int b;@public int c;@protected int d;@private int e;}publicAll code can accessprotectedAccess restricted to class & subclassesDefaultprivateAccess restricted to classpackageOnly available on 64-bit platformsMethod Access Levels24All methods are publicthere are ways to fake private methods if you are so inclinedDynamic Type Checking25Foo * test = [[Foo alloc] init];[test catMan];// Compile warning// Runtime error@interface Foo : NSObject {}- (void) bar;@endMethod calls are checked at runtimeThe compiler will look at methods you call and warn you if it thinks you are making a mistakeid26Pointer to object Rectangle * justRectangle = [[Rectangle alloc] init]; id anyThing = [[Rectangle alloc] init]; anyThing = @"a String";Recall that all objects are on the heap, so we always need a pointer to an object. id does not give the compiler any information about the type of the object.Identity versus Equality27Identity - pointer comparisonif (objectA == objectB )Equality - object attribute values are sameif ([objectA isEqual: objectB] )description28Like Java's toString@implementation Rectangle- (NSString*) description { return [NSString
View Full Document