Upload
sujith-krishnan
View
1.217
Download
4
Embed Size (px)
Citation preview
iPhone Apps A Closer Look
More about Obj-C• Object Creation
– Two step process• allocate memory
• initialize object
• + alloc– class method that allocates memory for the object
• - init– instance method to complete initialization
Creating ObjectsPerson *person = nil;
person = [[Person alloc] init]; OR
Person *person = [[Person alloc] init];
- (id)init {
// allow superclass to initialize its state first
if (self = [super init]) {
age = 0;
name = nil;
}
// do other initialization…
return self
}
Multiple init methods
Classes may define multiple init methods- (id)init;
- (id)initWithName:(NSString *)name;
- (id)initWithName:(NSString *)name age:(int)age;
person = [[Person alloc] init];
[person setName:@”MyName”];
[person setAge:24];
// Use ‘person’ object
// What is next ?[person release];
Reference countingEvery object has a “retainCount”
as long as retainCount is > 0,object is alive
+alloc and -copy create objects with retainCount 1
-retain increments retainCount
-release decrements retainCount
When retainCount reaches 0, object is destroyed
-dealloc method invoked
Object deallocation#import "Person.h"
@implementation Person
-(void)dealloc {
// Do any cleanup that’s necessary
// when we’re done, call super to clean us up
[super dealloc];
}
@end
You never call dealloc explicitly in your code
You only deal with alloc, copy, retain, release to manage an
object’s lifetime
iPhone v/s other patforms• Only one application at a time
• Only one window
• Limited Access• Limited Response Time
– Carefully craft Apps – Make sure data is not lost when user quits
• Limited System Resources– RAM 128MB, Capacity 8GB– ~70MB left for user applications– No swap file
iPhone v/s other patforms . . .• What you can ?
– Photo Library
– Camera
– Location Services– Address Book
– Accelerometer
– Many more . . .
ANATOMY OF AN XCODE PROJECT
• Main Components
– Classes– Other Sources– Resources– Framework– Product– Target