Iphone course 1

1,068 views

Published on

Published in: Technology, News & Politics
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,068
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
43
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Iphone course 1

  1. 1. iPhone Application Development 1 Janet Huang 2011/11/23
  2. 2. ALL Schedule 11/23 - iPhone SDK - Objective-C Basic - Your First iPhone Application 11/30 - MVC design & UI - GPS/MAP Application (CoreLocation & MapKit) - Google Map API - LBS Application 12/07 - Network service - Facebook API - LBS + Facebook Application
  3. 3. How to study? - Stanford CS193p - videos in iTunes U - all resources on website - Apple official document - Good book - iOS Programming The Big Nerd Ranch Guide
  4. 4. Today’s Topics• iPhone SDK• Objective-C• Common Foundation Class• Your First iPhone Application
  5. 5. iPhone SDK• Xcode Tools • Xcode • Instruments• iOS Simulator• iOS Developer Library
  6. 6. iPhone OS overview
  7. 7. Platform Components
  8. 8. OOP Vocabulary• Class: define the grouping of data and code, the “type” of an object• Instance: a specific allocation of a class• Method: a “function” that an object knows how to perform• Instance variable: a specific piece of data belonging to an object
  9. 9. OOP Vocabulary• Encapsulation • keep implementation private and separate from interface• Polymorphism • different object, same interface• Inheritance • hierarchical organization, share code, customize or extend behaviors
  10. 10. Inheritance- Hierarchical relation between classes- Subclass “inherit” behavior and data from superclass- Subclasses can use, augment or replace superclass methods
  11. 11. Objective-C• Classes & Objects• Messaging• Properties• Protocols
  12. 12. Classes and Instances• In obj-c, classes and instances are both objects• class is the blueprint to create instances
  13. 13. Classes and Objects• Classes declare state and behavior• State (data) is maintained using instance variables• Behavior is implemented using methods• instance variables typically hidden • accessible only using getter/setter methods
  14. 14. Define a Classpublic header private implementation#import <Foundation/Foundation.h> #import "Person.h"@interface Person : NSObject { @implementation Person // instance variables - (int)age { NSString *name; return age; int age; }} - (void)setAge:(int)value { // method declarations age = value; } - (NSString *)name; //... and other methods - (void)setName:(NSString *)value; @end - (int)age; - (void)setAge:(int)age; - (BOOL)canLegallyVote; - (void)castBallot;@end in .h file in .m file
  15. 15. A class declaration
  16. 16. Object Creation• Two steps • allocate memory to store the object • initialize object state +alloc class method that knows how much memory is needed -init instance method to set initial values, perform other setup
  17. 17. Implementing your own -init method Person *person = nil; person = [[Person alloc] init] #import “Person.h” @implementation Person - (id)init { if(self = [super init]){ age = 0; name = @”Janet”; // do other initialization } } Create = Allocate + Initialize
  18. 18. Messaging• Class method and instance method• Messaging syntax
  19. 19. Terminology• Message expression [receiver method: argument]• Message [receiver method: argument]• Selector [receiver method: argument]• Method The code selected by a message
  20. 20. Method declaration syntax
  21. 21. Class and Instance Methods• instances respond to instance methods - (id) init; - (float) height; - (void) walk;• classes respond to class methods + (id) alloc; + (id) person; + (Person *) sharedPerson;
  22. 22. Messaging • message syntax [receiver message] [receiver message:argument] [receiver message:arg1 andArg:arg2] • message declaration- (void)insertObject:(id)anObject atIndex:(NSUInteger)index; • call a method or messaging [myArray insertObject:anObject atIndex:0]
  23. 23. Instance Variables• Scope default @protected only the class and subclass can access @private only the class can access @public anyone can access• Scope syntax @interface MyObject : NSObject { int foo; @private int eye; @protected Protected: foo & bar int bar; Private: eye & jet @public int forum; Public: forum & apology int apology; @private int jet; }
  24. 24. • Forget everything on the previous slide! Mark all of your instance variables @private. Use @property and “dot notation” to access instance variables.
  25. 25. Accessor methods• Create getter/setter methods to access instance variable’s value @interface MyObject : NSObject { @private int eye; } * Note the capitalization - (int)eye; - instance variables always start with lower case - (void)setEye:(int)anInt; - the letter after “set” MUST be capitalized @end• Now anyone can access your instance variable using “dot syntax” someObject.eye = newEyeValue; // set the instance variable int eyeValue = someObject.eye; // get the instance variable’s current value
  26. 26. Properties@propertyLet compiler to help you generate setter/getter methoddeclarations@interface MyObject : NSObject { @interface MyObject : NSObject {@private @privateint eye; int eye;} }@property int eye; @property int eye;- (int)eye;- (void)setEye:(int)anInt; @end@end
  27. 27. Properties• An @property doesn’t have to match an instance variable@interface MyObject : NSObject { @interface MyObject : NSObject {@private }int p_eye; @property int eye;} @end@property int eye;@end *They are all perfectly legal!
  28. 28. Properties • Don’t forget to implement it after you declarein .h file in .m file @implementation MyObject@interface MyObject : NSObject { - (int)eye {@private return eye;int eye; }} - (void)setEye:(int)anInt {@property int eye; eye = anInt;@end } @end
  29. 29. Properties @synthesize Let compiler to help you with implementationin .h file in .m file@interface MyObject : NSObject { @implementation MyObject@private @synthesize eye;int eye; - (int)eye {} return eye;@property int eye; }@end - (void)setEye:(int)anInt { eye = anInt; } @end
  30. 30. Properties• Be careful!! What’s wrong? - (void)setEye:(int)anInt { self.eye = anInt; } Infinite loop!!! :( Can happen with the getter too ... - (int)eye { if (self.eye > 0) { return eye; } else { return -1; } }
  31. 31. Protocols@interface MyClass : NSObject <UIApplicationDelegate,AnotherProtocol> {}@end@protocol MyProtocol- (void)myProtocolMethod;@end
  32. 32. Dynamic and static typing• Dynamically-typed object id anObject not id *• Statically-typed object Person * anObject
  33. 33. The null pointer: nil• explicitly if (person == nil) return;• implicitly if (!person) return;• assignment person = nil;• argument [button setTarget: nil];• send a message to nil person = nil; [person castBallot];
  34. 34. BOOL typedef• Obj-C uses a typedef to define BOOL as a type• use YES or NO BOOL flag = NO; if (flag == YES) if (flag) if (!flag) if (flag != YES) flag = YES; flag = 1;
  35. 35. Foundation Framework• NSObject• Strings • NSString • NSMutableString• Collections • Array • Dictionary • Set
  36. 36. NSObject • Root class • Implements many basics • memory management • introspection • object equality- (NSString *)description is a useful method to override (it’s %@ in NSLog()).
  37. 37. String Constants• C constant strings “c simple strings”• Obj-C constant strings @“obj-c simple strings”• Constant strings are NSString instances NSString *aString = @“Hello World!”;
  38. 38. Format Strings• use %@ to add objects (similar to printf)NSString *aString = @”World!”;NSString *result = [NSString stringWithFormat: @”Hello %@”,aString];result: Hello World!• used for loggingNSLog(@”I am a %@, I have %d items.”, [array className], [array count]);Log output: I am NSArray, I have 5 items.
  39. 39. NSString• Create an Obj-C string from a C string NSString *fromCString = [NSString stringWithCString:"A C string" encoding:NSASCIIStringEncoding];• Modify an existing string to be a new string - (NSString *)stringByAppendingString:(NSString *)string; - (NSString *)stringByAppendingFormat:(NSString *)string; - (NSString *)stringByDeletingPathComponent; for example: NSString *myString = @”Hello”; NSString *fullString; fullString = [myString stringByAppendingString:@” world!”];
  40. 40. NSMutableString• Mutable version of NSString• Allows a string to be modified• Common methods + (id)string; - (void)appendString:(NSString *)string; - (void)appendFormat:(NSString *)format, ...;for example: NSMutableString *newString = [NSMutableString string]; [newString appendString:@”Hi”]; [newString appendFormat:@”, my favorite number is: %d”, [self favoriteNumber]];
  41. 41. MVC should did will target controller outlet countNotification de data da & KVO le ta ga action te so urc es model view
  42. 42. General process for buildingiPhone application 1.  Create  a  simple  MVC  iPhone  applica5on 2.  Build  interfaces  using  Interface  builder 3.  Declara5ons a.  Declaring  instance  variables b.  Declaring  methods 4.  Make  connec5ons a.  SeDng  a  pointer b.  SeDng  targets  and  ac5ons 5.  Implemen5ng  methods a.  Ini5al  method b.  Ac5on  methods 6.  Build  and  run  on  the  simulator 7.  Test  applica5on  on  the  device

×