9
iPhone Apps A Closer Look

Closer Look - iPhone programming

Embed Size (px)

Citation preview

Page 1: Closer Look - iPhone programming

iPhone Apps A Closer Look

Page 2: Closer Look - iPhone programming

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

Page 3: Closer Look - iPhone programming

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

}

Page 4: Closer Look - iPhone programming

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];

Page 5: Closer Look - iPhone programming

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

Page 6: Closer Look - iPhone programming

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

Page 7: Closer Look - iPhone programming

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

Page 8: Closer Look - iPhone programming

iPhone v/s other patforms . . .• What you can ?

– Photo Library

– Camera

– Location Services– Address Book

– Accelerometer

– Many more . . .

Page 9: Closer Look - iPhone programming

ANATOMY OF AN XCODE PROJECT

• Main Components

– Classes– Other Sources– Resources– Framework– Product– Target