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. 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. Today’s Topics
• iPhone SDK
• Objective-C
• Common Foundation Class
• Your First iPhone Application
12. 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
13. OOP Vocabulary
• Encapsulation
• keep implementation private and separate from
interface
• Polymorphism
• different object, same interface
• Inheritance
• hierarchical organization, share code, customize
or extend behaviors
14. Inheritance
- Hierarchical relation between classes
- Subclass “inherit” behavior and data from superclass
- Subclasses can use, augment or replace superclass methods
16. Classes and Instances
• In obj-c, classes and instances are both
objects
• class is the blueprint to create instances
17. 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
18. Define a Class
public 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
20. 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
21. 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
27. 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;
}
28. • Forget everything on the previous slide!
Mark all of your instance variables @private.
Use @property and “dot notation” to access instance variables.
29. 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
30. Properties
@property
Let compiler to help you generate setter/getter method
declarations
@interface MyObject : NSObject { @interface MyObject : NSObject {
@private @private
int eye; int eye;
} }
@property int eye; @property int eye;
- (int)eye;
- (void)setEye:(int)anInt; @end
@end
31. 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!
32. Properties
• Don’t forget to implement it after you
declare
in .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
33. Properties
@synthesize
Let compiler to help you with implementation
in .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
34. 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; } }
36. Dynamic and static
typing
• Dynamically-typed object
id anObject not id *
• Statically-typed object
Person * anObject
37. 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];
38. 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;
40. NSObject
• Root class
• Implements many basics
• memory management
• introspection
• object equality
- (NSString *)description is a useful method to override (it’s %@ in NSLog()).
42. Format Strings
• use %@ to add objects (similar to printf)
NSString *aString = @”World!”;
NSString *result = [NSString stringWithFormat: @”Hello %@”,
aString];
result: Hello World!
• used for logging
NSLog(@”I am a %@, I have %d items.”, [array className], [array count]);
Log output: I am NSArray, I have 5 items.
43. 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!”];
44. 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]];
45. MVC
should
did
will target
controller
outlet
count
Notification
de
data
da
& KVO
le
ta
ga
action
te
so
urc
es
model view
46. General process for building
iPhone 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