81
Getting Started Introduction to iPhone Development IAP 2010 edward benson / [email protected] iphonedev.csail.mit.edu Monday, January 11, 2010

Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Getting Started

Introduction to iPhone DevelopmentIAP 2010 ❄

edward benson / [email protected]

Monday, January 11, 2010

Page 2: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Today

• The Toolchain

• Starting a Project

• iPhone Application Structure

• Objective-C Crash Course

•Data Persistence with CoreData

Monday, January 11, 2010

Page 3: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

The iPhone Toolchain

Monday, January 11, 2010

Page 4: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

XCodeObjective-C, GDB

Interface BuilderGraphical UI Development

InstrumentsProfiling, Leak Finding

SimulatorTesting

Monday, January 11, 2010

Page 5: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Starting a Project

Monday, January 11, 2010

Page 6: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Monday, January 11, 2010

Page 7: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Your Project

Monday, January 11, 2010

Page 8: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

iPhone Simulator

Monday, January 11, 2010

Page 9: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

The Debug View

Monday, January 11, 2010

Page 10: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

The Debug View

Monday, January 11, 2010

Page 11: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

The iPhone Application Structure

Monday, January 11, 2010

Page 12: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Linked FrameworksGraphics, sound, bluetooth, etc

Monday, January 11, 2010

Page 13: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

The Executable

Monday, January 11, 2010

Page 14: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Targets (different build settings)

Monday, January 11, 2010

Page 15: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

ResourcesImages, sounds, data, IB files

Monday, January 11, 2010

Page 16: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Boilerplate Code

Monday, January 11, 2010

Page 17: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Your App’s Code

These folders, called groups are just abstractions to help you organize your project -- they don’t even exist in the filesystem.

Rearrange however you want.

Monday, January 11, 2010

Page 18: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

UI-Driven Programming

Nearly everything in your entire project is essentially just a callback.

int main(int argc, char *argv[]) { NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; int retVal = UIApplicationMain(argc, argv, nil, nil); [pool release]; return retVal;}

This is the entire main routine!

main.m

Monday, January 11, 2010

Page 19: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

UI-Driven Programming

So where is your hook to implement code?

... the applicationDidFinishLaunching callback

- (void)applicationDidFinishLaunching:(UIApplication *)application { // Override point for customization after app launch

! [window makeKeyAndVisible];}

UntitledAppDelegate.m

Monday, January 11, 2010

Page 20: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

UI-Driven Programming

UIApplication

UIAppDelegate- (void)applicationDidFinishLaunching:(UIApplication *)application {

[window makeKeyAndVisible];}

UI Driven

Initialize your User Interface

After which point your app is almost entirely

Monday, January 11, 2010

Page 21: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Objective-C

Monday, January 11, 2010

Page 22: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

C

Smalltalk-80

Objective C

Simula 67

C++

Objective C 2.0

2006

1983

1983

1980

1971

1967

Perl1987

Ruby1993

Java1995

Python1991

History

Source: Computer Languages Timelinehttp://www.levenez.com/lang/

Monday, January 11, 2010

Page 23: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Objective-C

• Primitives & Strings

• Objects, Messages, and Properties

• Memory Management

Monday, January 11, 2010

Page 24: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Primitives

The usual C Types

int, float, ...

It’s own boolean (ObjC forked before C99)

BOOL

Some special types

id, Class, SEL, IMP

Takes values NO=0 and YES=1

nil is used instead of null.

Monday, January 11, 2010

Page 25: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Strings

Always use (NSString *) instead of C Strings unless you know what you’re doing!!

Inline

Assigned

If you accidentally leave out the @, expect to crash!

@"This is an inline string";

NSString *str = @"This is assigned to a variable";

Monday, January 11, 2010

Page 26: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

NSLog

While you’re getting to know Objective-C,

NSLog

is your best friend.

(Or just use the debugger)

Monday, January 11, 2010

Page 27: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Exercise 1 - See, it’s like C

- (void)applicationDidFinishLaunching:(UIApplication *)application { ! int i;! for (i=0; i<10; i++) {! ! NSLog(@"Hello, word!");! }!! [window makeKeyAndVisible];

}

Monday, January 11, 2010

Page 28: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Exercise 2 - Broken strings and printf-style logging

1) Remove the @ before the string and see what happens

2) Try NSLog(@"Hello, word! %i", i);

Monday, January 11, 2010

Page 29: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Overview

• Primitives & Strings

• Objects, Messages, and Properties

• Memory Management

Monday, January 11, 2010

Page 30: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Declaring

Monday, January 11, 2010

Page 31: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Objects

.h

.m

.h Interface

Protocol

Implementation

Monday, January 11, 2010

Page 32: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Name it RPSGameMonday, January 11, 2010

Page 33: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Exercise 3 - Creating an object

#import "RPSGame.h"

Back in the app delegate...

And then

!RPSGame *game = [[RPSGame alloc] init];!NSLog(@"I have a game: %@", game);

Monday, January 11, 2010

Page 34: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Objects - Typing

Every object is of type

idThis is a pointer to the instance data of the object.

Of course, you can also declare a more specific type.

id game;

RPSGame * game;

Monday, January 11, 2010

Page 35: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

!RPSGame *game = [[RPSGame alloc] init];

!id game = [[RPSGame alloc] init];

Equivalent Statements

Monday, January 11, 2010

Page 36: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Methods and Messages

Monday, January 11, 2010

Page 37: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Messages

Method Calling v. Message Passing

Monday, January 11, 2010

Page 38: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Messages

[object message];

With no arguments

Monday, January 11, 2010

Page 39: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Messages

[object message];

[object message:value];

With no arguments

With 1 arguments

Monday, January 11, 2010

Page 40: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Messages

[object message];

[object message:value];

With no arguments

With 1 arguments

With 2 arguments[object message:value arg2:value];

Monday, January 11, 2010

Page 41: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Messages

[aPerson init];

[aPerson initWithFirst:@”Ted”];

With no arguments

With 1 arguments

With 2 arguments[aPerson initWithFirstAndLast:@”Ted” last:@”Benson”];

Monday, January 11, 2010

Page 42: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

You can send messages to classes

[Person alloc];

You can nest messages

Person* p = [[Person alloc] initWithName:@”Ted”];

Person* p = [Person alloc];[p initWithName:@”Ted”];

equal to

Monday, January 11, 2010

Page 43: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Defining Methods

- (id)initWithFirstAndLast:(NSString*)firstName last:(NSString*)lastName;

To Call[aPerson initWithFirstAndLast:@”Ted” last:@”Benson”];

To Define

Monday, January 11, 2010

Page 44: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

@implementation RPSGame

-(NSString *)getWinnerName {! return @"Ted";}

@end

@interface RPSGame : NSObject {}

-(NSString *)getWinnerName;

@end

Exercise 4 - A simple method, a simple message

NSLog(@"The winner was: %@", [game getWinnerName]);

RPSGame.h RPSGame.m

App Delegate

Monday, January 11, 2010

Page 45: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Instance Variables

Monday, January 11, 2010

Page 46: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

@interface RPSGame : NSObject {! NSString *winnerName;! NSString *loserName;}

! int someInt;! float someFloat;! id untypedObject;! // etc etc

Monday, January 11, 2010

Page 47: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Initialization

Monday, January 11, 2010

Page 48: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

The init convention

•Objective-C has a lot of conventions that are only enforced by its programmers, not the compiler

•Unfortunately, you just have to learn these

[[RPSGame alloc] init]

+(id)alloc;

-(id)init;

Allocates memory and returns a pointer.

Initializes the newly allocated object.

Monday, January 11, 2010

Page 49: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

The init convention

-(id)init;[[RPSGame alloc] init]

-(id)initWithAwesomeness:(BOOL)isAwesome;[[RPSGame alloc] initWithAwesomeness:YES]

-(id)initWithPlayer1:(NSString *)p1 player2:(NSString *)p2;[[RPSGame alloc] initWithPlayer1:@"Mario" player2:@"Luigi"]

Monday, January 11, 2010

Page 50: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

@implementation RPSGame

-(id)init {! if (self = [super init]) {! ! winnerName = nil;! ! loserName = nil;! }! return self;}

-(NSString *)getWinnerName {! return winnerName;}

@interface RPSGame : NSObject {! NSString *winnerName;! NSString *loserName;!}

-(id)init;

Exercise 5 - Initialization

RPSGame.h RPSGame.m

Monday, January 11, 2010

Page 51: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Exercise 6 - Mutators

-(NSString *)setWinnerName:(NSString *)name;

-(NSString *)setWinnerName:(NSString *)name {! winnerName = [name copy];}

! [game setWinnerName:@"Mario"];!

Monday, January 11, 2010

Page 52: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Properties

Monday, January 11, 2010

Page 53: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Properties

@interface TodoItem : NSObject {

int dbkey; BOOL complete; int priority; NSString * title; NSDate * due;}

@end

These all need getters and setters.

Monday, January 11, 2010

Page 54: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Writing getters and setters is annoying.

Answer: Properties.

Think of them as compiler macros that generate the getter and setter for you.

Monday, January 11, 2010

Page 55: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

@property (nonatomic, copy) NSString *winnerName;

@synthesize winnerName, loserName;

Interface

Implementation

Monday, January 11, 2010

Page 56: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Properties

@interface TodoItem : NSObject { int dbkey; NSString * title;}

@property (readonly) int dbkey;@property (nonatomic, retain) NSString *title;

@end

#import "TodoItem.h"

@implementation TodoItem

@synthesize title, dbkey;

@endYou are still responsible for

cleaning up memory for this object!

Monday, January 11, 2010

Page 57: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Property Attributes

@property (attributes) type name;

Writabilityreadwrite (default)readonly

Setter Semanticsassign (default)retaincopy

Atomicitynonatomic(no “atomic” attribute but this is the default)

http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_5_section_3.html

Source

Monday, January 11, 2010

Page 58: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Calling Properties

game.winnerName = @"Something";

a = game.winnerName;

@property (nonatomic, copy) NSString *winnerName;

Will allow you to use “dot notation”

Or message passing

[game setWinnerName:@"Something"];

a = [game getWinnerName];

Monday, January 11, 2010

Page 59: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

@interface RPSGame : NSObject {! NSString *winnerName;! NSString *loserName;!}

-(id)init;

@property (nonatomic, copy) NSString *winnerName;@property (nonatomic, copy) NSString *loserName;

@end

@implementation RPSGame@synthesize winnerName, loserName;

Exercise 7 - Properties

Change the AppDelegate to use “dot” notation.

Implementation

Interface

Monday, January 11, 2010

Page 60: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Recap

ObjectsInstance Variables

MethodsMessagesProperties

Monday, January 11, 2010

Page 61: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Overview

• Primitives & Strings

• Objects, Messages, and Properties

• Memory Management

(if you’re coming from a Python/Java/C# background, this is where things can get tricky)

Monday, January 11, 2010

Page 62: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Basic Idea

You need to help the Garbage Collector know when it is allowed to clean up an object.

Objective-C accomplishes this with a technique similar to reference counting.

Monday, January 11, 2010

Page 63: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Memory Management

Object Lifecycle

+ alloc - initOp - retain - release - release

Create array Initialize

Retain for use

main()

some_func() Release from use

Release from use

Monday, January 11, 2010

Page 64: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Memory Management

Image Credit: blog.tice.de

1

Object Lifecycle

+ alloc - initOp

Ref Count +1

- retain - release - release

Create array Initialize

Retain for use

main()

some_func() Release from use

Release from use

+1 -1 -11 2 1 0

Monday, January 11, 2010

Page 65: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Recall creating an object

Almost always follows the pattern

TodoItem *item = [[TodoItem alloc] init];

+ alloc

- init

Allocates the memory

Performs the initialization

..So leaves you with a retain count of 1

Monday, January 11, 2010

Page 66: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Exercise 7 - Retain Count

! NSLog(@"The game's retain count is: %i", [game retainCount]);

In the App Delegate...

! [game release];! NSLog(@"The game's retain count is: %i", [game retainCount]);

Now try:

Why does it crash?

Monday, January 11, 2010

Page 67: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Most important commands that affect retain count

+alloc

-copy

-retain

-release

-autorelease

+1

+1

+1

-1

-1

Creating a new object

Duplicating an object

Reserving an object for your use

Releasing an object from your use

Delayed release

Monday, January 11, 2010

Page 68: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Best way to think about it

Forget about the count!It means nothing to you, because the runtime will do

crazy things to it.

Instead, think of ownership

When you want an object, retain (or alloc) it.When you are done with an object, release it.

Monday, January 11, 2010

Page 69: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

So in our app delegate

! RPSGame *game = [[RPSGame alloc] init];! NSLog(@"I have a game: %@", game);

! [game setWinnerName:@"Mario"];!! NSLog(@"The winner was: %@", [game getWinnerName]);

! [game release];

+1

-1

“I want an RPSGame”

“OK, I’m done with the RPS Game”

If you just follow that mindset, you’ll be memory leak free. But you must be vigilant!

Monday, January 11, 2010

Page 70: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Autorelease

Monday, January 11, 2010

Page 71: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Sometimes, you are done with an object(so should release it!)

But you also want to return the object from a method.

Monday, January 11, 2010

Page 72: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Exercise 8 - Why do we need autorelease

-(RPSGame *)createGame {! RPSGame *game = [[RPSGame alloc] init];! [game release];! return game;}

In the App Delegate...

! RPSGame *game = [self createGame];

Now create your game like this:

Why does it crash?

Monday, January 11, 2010

Page 73: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

autorelease is like a delayed version of release.

It gives other parts of the code time to claim ownership of an object before it is swept up by the

GC process.

Monday, January 11, 2010

Page 74: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Exercise 9 - Using autorelease

-(RPSGame *)createGame {! RPSGame *game = [[RPSGame alloc] init];! return [game autorelease];}

In the App Delegate...

! RPSGame *game = [self createGame];

Now create your game like this:

But this still isn’t safe.... why?

Monday, January 11, 2010

Page 75: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Exercise 9 - Using autorelease

! RPSGame *game = [[self createGame] retain];

! .....

! [game release];

In the App Delegate...

Claim ownership

Release ownership

Monday, January 11, 2010

Page 76: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Deconstructors

Monday, January 11, 2010

Page 77: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

When is an object destroyed?

When it’s retain count reaches 0

Then the deconstructor is called - dealloc

Never call dealloc yourself -- this is always called automatically for you.(Except when you’re calling [super dealloc] from within your dealloc implementation)

Monday, January 11, 2010

Page 78: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Exercise 10 - Fix our deconstructor

-(void)dealloc {! [super dealloc];! [winnerName release];! [loserName release];}

In RPSGame.m

Monday, January 11, 2010

Page 79: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Phew!

Monday, January 11, 2010

Page 80: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Tomorrow we start the iPhone part

Monday, January 11, 2010

Page 81: Getting Started - courses.csail.mit.educourses.csail.mit.edu/iphonedev/slides/2010_01_ObjectiveC.pdf · XCode Objective-C, GDB Interface Builder Graphical UI Development Instruments

Great Objective C Resources

• Cocoa Dev Centralhttp://cocoadevcentral.com/d/learn_objectivec/

• The Objective-C 2.0 Programming Languagehttp://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/ObjC.pdf

• Stanford’s CS 193http://www.stanford.edu/class/cs193p/cgi-bin/index.php

• BYU’s CocoaHeads Chapterhttp://cocoaheads.byu.edu/resources

Monday, January 11, 2010