Lecture 03 Iphone Programming
Lecture 03 Iphone Programming
Spring 2010
Homework
Strongly recommend trying a submission today even if you’re not yet done
Multiple attempted submissions is no problem
Assignments are to be done individually (except approved pairs for final project)
Honor code applies
Any questions about the homework?
E-mail
Questions are best sent to [email protected]
Sending directly to instructor or TA’s risks slow response.
Web Site
Very Important!
http://cs193p.stanford.edu
All lectures, assignments, code, etc. will be there.
This site will be your best friend when it comes to getting info.
Sonali
Friday 11am to 1pm
Thursday 1pm to 3pm
Gates B26B
Foundation Framework
NSObject, NSString, NSMutableString
NSNumber, NSValue, NSData, NSDate
NSArray, NSDictionary, NSSet
NSUserDefaults, etc.
Memory Management
Allocating and initializing objects
Reference Counting
- (void)setOperand:(double)anOperand;
- (double)performOperation:(NSString *)operation;
@end
@implementation CalculatorBrain
- (void)setOperand:(double)anOperand
{
}
- (double)performOperation:(NSString *)operation
{
}
@end
1. Specify class method with + or instance method with - (more on this later)
2. Then return type in parentheses (can be void if returns nothing)
3. Then first part of name of method, always ending in : unless zero args
4. Then type of first argument in parentheses
5. Then name of first argument
6. If more arguments, next comes a required space, then repeat 3-5
7. Then semicolon if declaring , or code inside { } if implementing
Spaces allowed between any of the above steps, but no spaces in step 3 or 5
Calling syntax:
BOOL destroyed = [ship dropBomb:bombType at:dropPoint];
Infinite loop!
Vehicle *v = s;
[v shoot]; // compiler will warn, no crash at runtime in this case
id obj = ...;
[obj shoot]; // compiler won’t warn, might crash
[obj lskdfjslkfjslfkj]; // compiler will warn, runtime crash
[(id)someVehicle shoot]; // no warning, might crash
isMemberOfClass:
Can be sent to any object but does NOT take inheritance into account
Syntax: if ([obj isMemberOfClass:[NSString class]]) {
This would be false if obj were NSMutableString
className
Returns the name of the class as an NSString
Syntax: NSString *name = [obj className];
performSelector:
Send a message to an NSObject using a selector
Syntax: [obj performSelector:@selector(shoot)]
Syntax: [obj performSelector:@selector(foo:) withObject:x]
id anObject = ...;
SEL aSelector = @selector(someMethod:);
if ([anObject respondsToSelector:aSelector]) {
[anObject performSelector:aSelector withObject:self];
}
NSString
International (any language) strings
Used throughout all of iPhone SDK instead of C’s char *
Cannot be modified!
Usually returns you a new NSString when you ask it to do something
Tons of utility functions (case, URLs, conversion to/from lots of other
types, substrings, file system paths, and much much more!)
Compiler will create a constant one for you with @”foo”
NSMutableString
Mutable version of NSString
Can do some of the things NSString can do without making a new one
NSValue
Generic object wrapper for any non-object data type.
Useful for wrapping graphics things like a CGPoint or CGRect
NSData
“Bag of bits”
Used to save/restore/transmit data throughout the SDK
NSDate
Can be used to find out the time now or store a past/future time
See also NSCalendar, NSDateFormatter, NSDateComponents