Ios development

58
IOS Development game

description

it is a course summery using the iPhone Application Development - Stanford Winter 2010 - CS193P materials.

Transcript of Ios development

Page 1: Ios development

IOS Development game

Page 2: Ios development

iPhone OS Overview

Page 3: Ios development
Page 4: Ios development
Page 5: Ios development
Page 6: Ios development

Cocoa Touch Architecture

Page 7: Ios development

Objects

Page 8: Ios development

Object

Page 9: Ios development

Behavior

Page 10: Ios development

Message

Page 11: Ios development

State

Page 12: Ios development

Other Objects As State

Page 13: Ios development

Outlets

Page 14: Ios development

Target / Action

Page 15: Ios development

Demo

Page 16: Ios development

OOP Vocabulary

Class

Instance

Method

Instance Variable

Encapsulation

Polymorphism

Inheritance

Page 17: Ios development

Inheritance

Page 18: Ios development

Objective-C

Page 19: Ios development

Objective-C

Strict superset of C Mix C with ObjC

Or even C++ with ObjC (usually referred to as ObjC++)

Single inheritance

Loosely typed, if you’d like

Page 20: Ios development

Dynamic Runtime

Object creationAll objects allocated out of the heap

No stack based objects

Message dispatch

Page 21: Ios development

Class and Instance Methods

Instances respond to instance methods (-)

Classes respond to class methods (+)

Page 22: Ios development

Message syntax

[receiver message]

[receiver message:argument]

[receiver message:arg1 andArg:arg2]

Page 23: Ios development

Demo

Show Class creation

Instance variable

Setter and getter

Message Examples

Method definition examples

Selector Example and messaging it

Page 24: Ios development

Dot SyntaxObjective-C 2.0 introduced dot syntax

Convenient shorthand for invoking accessor methods

float height = [person height];

float height = person.height;

[person setHeight:newHeight];

person.height = newHeight;

Follows the dots...

[[person child] setHeight:newHeight]; // exactly the same as person.child.height = newHeight;

Page 25: Ios development

Objective-C Types

Page 26: Ios development

Dynamic and static typing

Dynamically-typed object

Statically-typed object

Page 27: Ios development

The null object pointer

Test for nil explicitly

if (person == nil) return;

Or implicitly

if (!person) return;

Can use in assignments and as arguments if expected

Sending a message to nil?

Page 28: Ios development

BOOL typedef

When ObjC was developed, C had no boolean type (C99 introduced one)

ObjC uses a typedef to define BOOL as a type

Macros included for initialization and comparison: YES and NO

Page 29: Ios development

Selectors identify methods by name

A selector has type SEL

SEL action = [button action]; ![button setAction:@selector(start:)];

Conceptually similar to function pointer

Selectors include the name and all colons, for example:

-(void)setName:(NSString *)name age:(int)age;

would have a selector:

SEL sel = @selector(setName:age:);

Page 30: Ios development

Demo

Using selector and respondsToSelector: method

Page 31: Ios development

Class Introspection

You can ask an object about its class

Testing for general class membership (subclasses included):

Testing for specific class membership (subclasses excluded):

Page 32: Ios development

Object Identity versus Equality

Identity—testing equality of the pointer values

Equality—testing object attributes

Page 33: Ios development

Demo

Class Introspection

Instance equality

Page 34: Ios development

Foundation Classes

Page 35: Ios development

NSObject

Root class

Implements many basics Memory management

Introspection

Object equality

Page 36: Ios development

Other Types

NSString

String Constants In C constant strings are

“simple” In ObjC, constant strings are

@“just as simple” Constant strings are NSString instances

NSString *aString = @”Hello World!”;

Page 37: Ios development

Format Strings

NSMutableString

Collections

NSArray

NSMutableArray

NSDictionary

NSMutableDictionary

NSSet

NSMutableSet

Page 38: Ios development

Enumeration

Consistent way of enumerating over objects in collections

Use with NSArray, NSDictionary, NSSet, etc.

for (i = 0; i < count; i++) {

for (Person *person in array) {

Page 39: Ios development

Demo

Enumeration

NSString

Page 40: Ios development

Object Lifecycle

Page 41: Ios development

Object Creation

Two step processallocate memory to store the object

initialize object state

+ alloc

Class method that knows how much memory is needed

- init

Instance method to set initial values, perform other setup

Create = Allocate + Initialize

Page 42: Ios development

Implementing your own -init method

Demo

Multiple init methods

Page 43: Ios development

Memory Management

Allocation Destruction

C malloc free

Objective-C alloc dealloc

Calls must be balanced

Otherwise your program may leak or crash

Page 44: Ios development

Reference Counting

Every object has a retain count Defined on NSObject

As long as retain count is > 0, object is alive and valid

+alloc and -copy create objects with retain count == 1

-retain increments retain count

-release decrements retain count

When retain count reaches 0, object is destroyed -dealloc method invoked automatically

One-way street, once you’re in -dealloc there’s no turning back

Page 45: Ios development

Demo

Balanced Call

Page 46: Ios development

Reference counting in action

Person *person = [[Person alloc] init];

Retain count begins at 1 with +alloc

[person retain];

Retain count increases to 2 with -retain

[person release];

Retain count decreases to 1 with -release[person release];

Retain count decreases to 0, -dealloc automatically called

Page 47: Ios development

Messaging deallocated objectsDemo

Page 48: Ios development

Implementing a -dealloc method

Demo

Page 49: Ios development

Object Lifecycle Recap

Objects begin with a retain count of 1

Increase and decrease with -retain and -release

When retain count reaches 0, object deallocated automatically

You never call dealloc explicitly in your code

Exception is calling -[super dealloc]

You only deal with alloc, copy, retain, release

Page 50: Ios development

Autorelease

Calling -autorelease flags an object to be sent release at some point in the future

Let’s you fulfill your retain/release obligations while allowing an object some additional time to live

Makes it much more convenient to manage memory

Very useful in methods which return a newly created object

Page 51: Ios development

Demo

When to use auto release (return methods)

Page 52: Ios development

Method Names & Autorelease

Methods whose names includes alloc, copy, or new return a retained object that the caller needs to release

All other methods return autoreleased objects

This is a convention- follow it in methods you define!

Page 53: Ios development

How does -autorelease work?Object is added to current autorelease pool

Autorelease pools track objects scheduled to be released

When the pool itself is released, it sends -release to all its objects

UIKit automatically wraps a pool around every event dispatch

Page 54: Ios development

Hanging Onto an Autoreleased Object

Many methods return autoreleased objects

If you need to hold onto those objects you need to retain them

Page 55: Ios development

Demo

Return auto released object and retain it.

Page 56: Ios development

Objective-C Properties

Page 57: Ios development

Properties

Provide access to object attributes

Shortcut to implementing getter/setter methods

Also allow you to specify:read-only versus read-write access

memory management policy

Page 58: Ios development

Demo

Properties

Property Attributes