Cs193P - Lecture 2: Iphone Application Development
Cs193P - Lecture 2: Iphone Application Development
Objective-C
Foundation Framework
Announcements
• Enrollment process is complete!
• Troy Brant
■ Tuesdays 12-2pm: 4th floor of Gates 463
• Paul Salzman
■ Mondays 12-2: Gates B26A
■ Wednesday 4/8 (one-time only) 12-2pm: Gates 24A
Apple Design Awards
• Student categories for iPhone & Leopard apps
■ 16GB iPhone 3G
• http://developer.apple.com/wwdc/ada
iPhone SDK
• Assignment schedule:
■ Handed out on Mondays (correction from lecture)
■ Due the following Tuesdays, by 11:59pm (correction from lecture)
• Submitting Assignments:
■ Click on ‘Submissions’ tab on class website
■ Follow instructions
Memory
Superclass
NSObject management
Generic
UIControl behaviors
Specific
Subclass
UIButton UITextField behaviors
■ Selectors
• Message dispatch
• Introspection
OOP with ObjC
Classes and Instances
• In Objective-C, classes and instances are both objects
• Class is the blueprint to create instances
method
Class
method
method
method
Data
method
method
method
method methodData
method
method
method
method
method
Data methodData
method
method
method
methodData
method
method
method
methodData
method
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
OOP From ObjC Perspective
• Everybody has their own spin on OOP
■ Apple is no different
• For the spin on OOP from an ObjC perspective:
■ Read the “Object-Oriented Programming with Objective-C”
document
■ http://developer.apple.com/iphone/library/documentation/
Cocoa/Conceptual/OOP_ObjC
Messaging syntax
Class and Instance Methods
• Instances respond to instance methods
- (id)init;
- (float)height;
- (void)walk;
[receiver message:argument]
[voter castBallot];
[voter setAge:21];
if ([voter canLegallyVote]) {
// do something voter-y
}
• Message
[receiver method: argument]
• Selector
[receiver method: argument]
• Method
The code selected by a message
Dot Syntax
• Objective-C 2.0 introduced dot syntax
• Convenient shorthand for invoking accessor methods
float height = [person height];
float height = person.height;
[person setHeight:newHeight];
person.height = newHeight;
• Statically-typed object
Person *anObject
■ Object equality
NSString
• General-purpose Unicode string support
■ Unicode is a coding system which represents all of the world’s
languages
• Consistently used throughout Cocoa Touch instead of “char *”
• Without doubt the most commonly used class
• Easy to support any language in the world with Cocoa
String Constants
• In C constant strings are
“simple”
• In ObjC, constant strings are
@“just as simple”
• Constant strings are NSString instances
NSString *aString = @”Hello World!”;
Format Strings
• Similar to printf, but with %@ added for objects
NSString *aString = @”Johnny”;
NSString *log = [NSString stringWithFormat: @”It’s ‘%@’”, aString];
• Example:
NSString *myString = @”Hello”;
NSString *fullString;
fullString = [myString stringByAppendingString:@” world!”];
• Example:
NSString *myString = @”Hello”;
NSString *otherString = @”449”;
if ([myString hasPrefix:@”He”]) {
// will make it here
}
if ([otherString intValue] > 500) {
// won’t make it here
}
NSMutableString
• NSMutableString subclasses NSString
• Allows a string to be modified
• Common NSMutableString methods
+ (id)string;
- (void)appendString:(NSString *)string;
- (void)appendFormat:(NSString *)format, ...;
// old school
Person *person;
int count = [array count];
for (i = 0; i < count; i++) {
person = [array objectAtIndex:i];
NSLog([person description]);
}
// new school
for (Person *person in array) {
NSLog([person description]);
}
NSNumber
• In Objective-C, you typically use standard C number types
• NSNumber is used to wrap C number types as objects
• Subclass of NSValue
• No mutable equivalent!
• Common NSNumber methods
+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber *)numberWithDouble:(double)value;
- (int)intValue;
- (double)doubleValue;
Other Classes
• NSData / NSMutableData
■ Arbitrary sets of bytes
• NSDate / NSCalendarDate
■ Times and dates
Getting some objects
• Until we talk about memory management:
■ Use class factory methods
■ NSString’s +stringWithFormat:
■ NSArray’s +array
■ NSDictionary’s +dictionary