2. iOS Overview
Cocoa Touch Core OS
Media
Core Services OS X Kernel Power management
Mach 3.0 Keychain Access
Core OS BSD Certificates
Sockets File System
Security Bonjour
2
3. iOS Overview
Cocoa Touch Core Services
Media
Core Services Collections Core Location
Address Book Net Services
Core OS Networking Threading
File Access Preferences
SQLite URL Utilities
3
4. iOS Overview
Cocoa Touch Media
Media
Core Services Core Audio JPEG, PNG, TIFF
OpenAL PDF
Core OS Audio Mixing Quartz (2D)
Audio Recording Core Animation
Video Playback OpenGL
4
5. iOS Overview
Cocoa Touch Cocoa Touch
Media
Multi-touch Alerts
Core Services
Core Motion Web View
Core OS View Hierarchy Map Kit
Localization Image Picker
Controls Camera
5
6. Platform Components
• Tools
• Language [display setTextColor:[UIColor blackColor]];
• Frameworks
• Design Strategies V
6
7. Model View Controller
Controller
Model View
Model – What your application is (but not how it is displayed)
Controller– How your model is presented to User (UI Logic)
View – Controller’s Minions
7
9. Application Life Cycle
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
9
10. Objective C
CalculatorViewController
Controller
action
x
7
Model View
4
+
operationPressed:
CalculatorModel UI Buttons
digitPressed:
10
11. Model
Header file
CalculatorModel.h
Model
Name of the class
#import <Foundation/Foundation.h>
Class’s Superclass
@interface CalculatorModel : NSObject
{
Instance Variable between the
double operand; paranthesis
}
Return Type
-(void) setOperand : (double) anOperand;
-(double) performOperation : (NSString*)operation;
Method declarations
Name of the method Method argument
@end
11
16. Objective C - Methods
- (NSArray *)shipsAtPoint:(CGPoint)bombLocation withDamage:(BOOL)damaged;
Instance Methods Class Methods
• Start with a dash ( - ) • Starts with a plus ( + ), used for
• Normal method singletons, allocation, utilities.
• Can access instance variables inside as if +(id) alloc;
they were locals • Can not access instance variable inside.
• can send message to self and super • Message to “Self” and “Super” mean
inside something a little different. Both invoke
• Example only other “class” methods. Inheritance
BOOL destroyed = [ ship does not work.
dropBomb:bombType at:dropPoint • Example
from:height]; CalculatorModel *model = [[
CalculatorModel alloc] init];
16
17. Objective C – Instance Variables
Scope
By default, instance variables are @protected (only the class and subclass can access).
Can be marked @private (only the class can access) or @public (anyone can access)
Scope Syntax
@interface MyObject: NSObject
{
int foo;
@private
int eye;
int jet;
@protected
int bar;
@public
int forum;
int xyz;
}
17
18. Obj C - Properties
• Now forget everything on the previous slide
•Mark all instance variable @private
• Use @property and dot to access instance variables
• Create methods to get/set an instance variable value
@interface MyObject : NSObject
{
@private
int eye;
} Letter after “set” MUST be capitalized,
otherwise dot notation will not work
- (int) eye;
- (void) setEye : (int) anInt;
@end
18
19. Obj C - Properties
• @property
Complier can generate get/set methods declaration with @property directive.
@interface MyObject : NSObject
{
@private
int eye;
}
@property int eye;
- (int) eye; hidden
- (void) setEye : (int) anInt;
@end
If “readonly” keyword is used only get method will
@property (readonly) int eye; be generated.
19
21. Obj C – Dynamic Binding
• All objects are allocated to heap, so use pointers
• It’s legal to “cast” a pointer.
• All objects inherited from NSObjects knows about
• isKindOfClass
• isMemberOfClass
• respondToSelector
21
22. Frameworks
Foundation Framework
• NSObject
• Base class for pretty much every object in the iOS SDK.
• Implements memory management primitives
• NSString
• Unicode
• Used throughout iOS instead of C language char *
• An NSString instance can not be modified. They are immutable
• NSMutableString
• Mutable version of NSString.
• NSNumber, NSData, NSDate
• NSArray, NSMutableArray
• NSDictionary, NSMutableDictionary
• NSSet, NSMutableSet
• NSUserDefaults
22
23. Creating Objects
Two Step process
-allocate
-Initiate
alloc makes space in heap for class’s instance variables.
Each class has a designated “initializer” method.
MyObject *obj = [ [ MyObject alloc] init];
23
24. Getting Objects
alloc/init is not the only way to “get” an object. Plenty of classes give the object if you ask
for it.
NSString *newDisplay = [ display.text stringByAppendingString:digit ];
NSArray *keys = [ dictionary allKeys];
Who frees the memory for all these objects?
No Garbage collection in iOS
Answer lies in “reference counting”
24
25. Reference Counting
-Take the ownership for an object you want to keep a pointer to
- When you are done with the object, you give up the ownership
- When no one claims ownership for an object, it gets de allocated.
So when do we take the ownership?
Own any object as soon as we send a msg with new, alloc, or copy. But if we receive the object from any other
source you don’t own it, but we can take ownership by sending it the NSObject message “retain”.
The object which we receive from any other source, we’ll own it temporarily until the call stack unwinds Or, the
object is retained.
Once we are done with the object, send the object the NSObject message “release”.
We now own theMoney, we got it using
the method alloc. We are responsible for
-(Money *) showMeTheMoney:(double) amount { sending it release
Money *theMoney = [[ Money alloc] init:amount];
[theMoney autorelease]; We’ve now released the object but it won’t happen
return Money; until the caller of this method has had chance to retain
to theMoney if they want.
}
Caller
Money *myMoney = [ bank showMeTheMoney:5000];
[myMoney retain];
25
26. Deallocation
What happens when the last owner calls release?
A special method, dealloc, is called on the object & the object’s memory is returned to the heap. After this
happens, sending a message to the object will crash the program.
Override “dealloc” in your class but NEVER call it !!! [ Strange ? ]
It only gets called by release when the last owner has ‘released’ the object. The one exception about calling it is
that you should call [super dealloc] in your dealloc.
Example
- (void) dealloc
{
[brain release];
[super dealloc];
}
26
27. @property
Example
display.text = [display.text stringByAppendingString:digit];
This is still owned by display (the UILabel). We don’t This is the NSString we acquired. It comes back
retain it because we are not going to keep it. We’re autoreleased. We’re not going to retain this one either
just going to use it to acquire a different NSString b’cause we’re just going to pass it on to display’s text
from stringByAppendingString property setter method.
Setting an instance variable via setter method (via property)
Did we retain that object? We need to retain instance variable and this can be done via setter methods but what about
@synthesis
There are 3 options for setters made by @synthesis
@property (retain) NSArray *myArrayProperty;
@property (copy) NSString *someNameProperty;
@property (assign) id delegate;
The first 2 options are straight forward. The third option means that neither retain nor copy is sent to the object passed to
the setter. This means that if this object is released by all other owners, we’ll have a bad pointer. So we only use “assign” if
the passed object essentially owns the object with the property.
A controller and its views (b’cause a View should never outlive its controller) A View can have a property (delegate is very
common) which is assigned to its controller.
27
28. @property examples
@property (retain) NSString *name;
@synthesis will create a setter equivalent to
-(void) setName:(NSString *) aString
@synthesis will release the retain
previous object (if any, could be
{ nil) before setting and retaining the
[name release]; new one.
name = [aString retain];
}
@property (copy) NSString *name;
@synthesis will create a setter equivalent to
copy -(void) setName:(NSString *) aString
{
[name release];
name = [aString copy];
}
@property (assign) NSString *name;
@synthesis will create a setter equivalent to
assign
-(void) setName:(NSString *) aString
{
name = aString;
}
28
29. #import <Foundation/Foundation.h> @protocol ProcessDataDelegate <NSObject> @required - (void) processSuccessful: (BOOL)success; @end
protocols
Protocols can be useful in a number of cases, a common usage is to define method that are to be implemented by other
classes.
Similar to @interface, but no implementation
@protocol Foo
-(void) doSomething; // implementors must implement
@optional
-(int) getSomething; // Optional to implement.
Class that implement will look like
@interfaceMyClass : NSObject <Foo>
…
@end
Use of protocols are mainly in delegates and dataSources
The delegate or dataSource is always defined as an assign @property
@property (assign) id <UISomeObjectDelegate> delegate;
29
31. Application Delegate
Class that adopts the protocol.
HappinessAppDelegate.h (header file for Application Delegate) This object implements the
UIApplicationDelegate protocol. The
applicationDidxxx and applicationWillxxx
@interface HappinessAppDelegate : NSObject <UIApplicationDelegate> methods
{
UIWindow *window;
Instance Variable/property/outlet
HappynessViewController *viewController; pointing to HappynessViewController
} (our controller) also hooked up to
MainWindow.xib
@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet HappynessViewController *viewController;
@end
Instance Variable/property/outlet pointing to the top-level view for this
application (UIWindow). This is hooked up to MainWindow.xib
31