2. Disclaimer
These slides are heavily based on Evan Doll and Alan Cannistraro’s
slides prepared for iPhone App Programming course @ Stanford
http://www.stanford.edu/class/cs193p
Look for iPhone Application Programming
in iTunes U for videopodcasts.
10. Objective-C
• Strict superset of C
‣ Mix C with Obj-C
• Object oriented language
• Based on message passing (similar to Smalltalk)
• No garbage collector need to manually manage memory
(not covered in this presentation)
12. Message Examples
BankAccount *myAccount; //assume this exists
int n = [myAccount accountNumber];
[myAccount deposit:100];
if([myAccount isNotEmpty]) {
[myAccount withdraw:50 printReceipt:YES];
}
13. Some new types
• Anonymous Objects
• Class
• ...others but no time!
(Selectors “SEL”, Boolean “BOOL”, etc.)
14. Anonymous Objects
• Normal (or Statically-typed) object
BankAccount *anAccount
• Anonymous (or Dynamically-typed) object
id anAccount
- Just id (Not id*)
- id is the data type that indicates that we are talking about an object
15. Class
• class instances responds to instance methods “-”
- (id)init;
- (double)accountNumber;
int n = [myAccount accountNumber];
• classes responds to class methods “+” (like Java “static” methods)
+ (id)alloc;
+ (float)stdInterestRate;
float rate = [BankAccount stdInterestRate];
16. Defining aaclass
Defining a class
Defining class
A public header and a private implementation
A public header and a private implementation
A public header and a private implementation
Header File File
Header
Header File Implementation File
Implementation File File
Implementation
17. Defining a class
Defining a class
Demo
A public header and a private implementation
A public header and a private implementation
BankAccount class
BankAccount.h
Header File File
Header BankAccount.m
Implementation File File
Implementation
21. MVC
• Only the Controller knows both Model and View
• Makes code more reusable (especially the Model part)
Interface Builder
Model View
Controller
Xcode