Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 3800 Su 11 Beg. IOS Dev L4 Obj C part 1 CS 3800 Introduction to IOS programming Summer 2011 G. Zimmerman.

Similar presentations


Presentation on theme: "CS 3800 Su 11 Beg. IOS Dev L4 Obj C part 1 CS 3800 Introduction to IOS programming Summer 2011 G. Zimmerman."— Presentation transcript:

1 CS 3800 Su 11 Beg. IOS Dev L4 Obj C part 1 CS 3800 Introduction to IOS programming Summer 2011 G. Zimmerman

2 CS 3800 Su 11 Beg. IOS Dev Announcements Survey says….. 016 passwords 016 Keys: Server? Today’s ‘Final’ code will be posted after class L4 Obj C part 1

3 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

4 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

5 CS 3800 Su 11 Beg. IOS Dev Objective-C Classes NSObject NSArray NSString NSSet NSDictionary NSDate Mutable & Immutable L4 Obj C part 1

6 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 http://0- proquest.safaribooksonline.com.mauric e.bgsu.edu/book/programming/objective -c/9780321605559

7 CS 3800 Su 11 Beg. IOS Dev l4 Obj-C part I XCode: MacOS command line template Tour

8 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

9 CS 3800 Su 11 Beg. IOS Dev l4 Obj-C part I Format Example float x = 14.9345; int age = 145; char* cname = "old style c string"; NSLog(@" print spec examples: 7.6f %7.6f ", x); NSLog(@" print spec examples: 5.2f %5.2f ", x); NSLog(@" print spec examples: 4.2f %4.2f ", x); NSLog(@" print spec examples: 3.2f %3.2f ", x); NSLog(@" name: %s, age:%4d", cname, age);

10 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 NSString @

11 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

12 CS 3800 Su 11 Beg. IOS Dev Example: Fraction class @interface @implementation L4 Obj C part 1

13 CS 3800 Su 11 Beg. IOS Dev Designated initializer -(id) initFractionWithNumerator:(NSInteger) aNumerator denominator:(NSInteger) aDenominator { self = [super init]; if ( self != nil) { NSLog(@" %@ ", __FUNCTION__); numerator = aNumerator; denominator = aDenominator; } return self; } L4 Obj C part 1

14 CS 3800 Su 11 Beg. IOS Dev description #pragma mark – #pragma mark NSObject overrides (NSString *) description { if ( denominator ==1 ) return [NSString stringWithFormat:@"%d", numerator]; //else return FILL IN THE BLANK ; } L4 Obj C part 1

15 CS 3800 Su 11 Beg. IOS Dev Make some fractions #import [ [ classname alloc ] init] Retain count L4 Obj C part 1

16 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

17 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

18 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

19 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

20 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

21 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.

22 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

23 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

24 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

25 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

26 CS 3800 Su 11 Beg. IOS Dev Property & Synthesize  More than: Automatic setters/getters of iVars  Exposing state in a safe way (inc: subclassing).  @property ( L4 Obj C part 1


Download ppt "CS 3800 Su 11 Beg. IOS Dev L4 Obj C part 1 CS 3800 Introduction to IOS programming Summer 2011 G. Zimmerman."

Similar presentations


Ads by Google