CS 3800 Su 11 Beg. IOS Dev L4 Obj C part 1 CS 3800 Introduction to IOS programming Summer 2011 G. Zimmerman
CS 3800 Su 11 Beg. IOS Dev Announcements Survey says… passwords 016 Keys: Server? Today’s ‘Final’ code will be posted after class L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev Objective-C Goal: cover enough fundamentals to form a backdrop for understanding iOS programming. If you continue with iOS, you will want to delve further. L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev Objective-C alloc init ivars and functions Multiple parameters Property, Synthesize L4 Obj C part 1 Memory management Protocols Categories Inheritance
CS 3800 Su 11 Beg. IOS Dev Objective-C Classes NSObject NSArray NSString NSSet NSDictionary NSDate Mutable & Immutable L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev l4 Obj-C part I Monday: A Fraction Class References: Objective C Beginner's guide RH CSSE 493 Podcast #8 proquest.safaribooksonline.com.mauric e.bgsu.edu/book/programming/objective -c/
CS 3800 Su 11 Beg. IOS Dev l4 Obj-C part I XCode: MacOS command line template Tour
CS 3800 Su 11 Beg. IOS Dev l4 Obj-C part I Format specifiers Google “C format specifiers” Like iomanip Appl: creating strings & general output Create a “format template” XX.XX 5 total spaces, 2 decimals –%5.2f Aside
CS 3800 Su 11 Beg. IOS Dev l4 Obj-C part I Format Example float x = ; int age = 145; char* cname = "old style c string"; print spec examples: 7.6f %7.6f ", x); print spec examples: 5.2f %5.2f ", x); print spec examples: 4.2f %4.2f ", x); print spec examples: 3.2f %3.2f ", x); name: %s, age:%4d", cname, age);
CS 3800 Su 11 Beg. IOS Dev l4 Obj-C part I Specifier Sampler Integer %I or %d Float %f %lf Char %c C-string %s
CS 3800 Su 11 Beg. IOS Dev C++ Fraction Class (partial) Recall from C++: class Fraction { public: Fraction(); Fraction( int, int); Fraction(int); private: int num, den; void normalize(); } L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev Example: L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev Designated initializer -(id) initFractionWithNumerator:(NSInteger) aNumerator denominator:(NSInteger) aDenominator { self = [super init]; if ( self != nil) { ", __FUNCTION__); numerator = aNumerator; denominator = aDenominator; } return self; } L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev description #pragma mark – #pragma mark NSObject overrides (NSString *) description { if ( denominator ==1 ) return [NSString numerator]; //else return FILL IN THE BLANK ; } L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev Make some fractions #import [ [ classname alloc ] init] Retain count L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev Standard representation -(void) normalize { if ( denominator <0) { numerator = -numerator; denominator = -denominator; } int a = abs(numerator); int b= abs(denominator); int t; while (b != 0) { t = b; b = a % b; a = t;} // a is the gcd numerator = numerator / a; denominator = denominator / a; } L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev Revised initializer -(id) initFractionWithNumerator:(NSInteger)aNum denominator:(NSInteger)aDen { if ( aDen == 0) return nil; self = [super init]; if ( self != nil ) { numerator = aNum; denominator = aDen; } [self normalize]; return self; } L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev Alternate initializers Convenience for clients Reuse designated intializer Whole number initWithWholeNumber: Default initializer: Init –For Default behavior L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev CLASS convenience methods Fraction *fx = [FRACTION zero]; Standard is to return an autoreleased object +(id) zero; +(id) one; +(id) negativeOne; +(id) fractionWithNumerator:(NSInteger)num denominator:(NSInteger)two; L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev Retain Counts (intro) Onbject: many owners. i.e. Many pointers to the one object. Fraction *x = [Fraction one]; Fraction *y = x; [y retain]; Fraction *z = y; [z retain]; L4 Obj C part 1 numerator: 1 denominator: 1 x y z
CS 3800 Su 11 Beg. IOS Dev Memory management Cardinal Rules L4 Obj C part 1 Only release or autorelease objects you own. You own it if: it was created with a method whose name begins: alloc new copy ( a few more) you send it a retain message.
CS 3800 Su 11 Beg. IOS Dev C++ getters/setters class Fraction { public: void setNumerator( int ); int getNumerator(); private: int num, den; void normalize(); } Fraction x; x.setNumerator(2); cout << “ The num is” << x.getNumerator(); L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev Objective C getters/setters Getters: (NSInteger) numerator { return numerator; } Setter: -(void) setNumerator { // spelling important ? } L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev Dot Notation 1 Fraction * f1 = ….. NSInteger top = [f1 numerator]; //getter OR NSInteger top = f1.numerator; // calls the getter f1.numerator IS [f1 numerator]; THIS IS NOT YOUR FATHER’S dot Notation! L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev Dot Notation 2 Fraction * f1 = ….. = [f1 setNumerator:14]; //setter OR f1.numerator = 14; // calls the setter Where did the Set go? L4 Obj C part 1
CS 3800 Su 11 Beg. IOS Dev Property & Synthesize More than: Automatic setters/getters of iVars Exposing state in a safe way (inc: subclassing). ( L4 Obj C part 1