ch 2 & 3 - objc objects and messages

15
Objectiv e-C Objects & Messages 44-590-3 Tuesday, January 17, 12

Upload: pamidi39

Post on 06-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 1/15

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 2/15

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 3/15

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 4/15

Sending a Message

• To send an object a message, enclose the object (the receiver ) and its message in [ ], as in [receivermessage]

• [airplane takeOff];

• // airplane is the receiver ; receives a takeOff message

• [airplane setName:@”United”]; // 1 parameter, @”United”

• [airplane addPassengers:150 crewMembers:5]; // 2 parameters

• [airplane setArrivalTime:1800 toGate:@”A3” withQuickTurnAroundNeeded:YES];

• If a method returns a value it passes through the [ ]

• BOOL response = [airplane internationalFlight]; // YES or NO

• The selector name concatenates all of its name parts, e.g.,

•takeOff 

• setName:

• addPassengers:crewMembers:

• setArrivalTime:toGate:withQuickTurnAroundNeeded:

• The : is significant, and the # of :’s always equals the number of parameters in the method declaration

Tuesday, January 17, 12

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 5/15

Declaring a Method-(void)takeOff;

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

-(void)addPassengers:(int)numPax crewMembers:(int)numCrew;

-(void)setArrivalTime:(int)eta toGate:(NSString *)gate withQuickTurnAround:(BOOL)qtaRequested;

-(BOOL)internationalFlight;

Tuesday, January 17, 12

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 6/15

Unrecognized Selectors

• Typos matter, but the compiler just givesyou a warning:

• [plane takeoff]; // ObjC is case sensitive

• At runtime, program crashes with anunrecognized selector error

• Moral: don’t ignore compiler warnings,ever!

Tuesday, January 17, 12

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 7/15

About that -

• - means an instance method

• it can access instance variables

• sent to objects

• + means a class method

• sent to the class

Tuesday, January 17, 12

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 8/15

Sending Class Messages

• Sending a class a message is just like sending anobject a message

• e.g.,

• +(void)describeCategory;

• [Airplane describeCategory];

• One very commonly used class method is alloc, usedto make an object

Tuesday, January 17, 12

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 9/15

Making an Object

• To make an object, we first have to allocate roomfor it, then send it an initialization message:

  Airplane * airplane = [Airplane alloc];[airplane init];[airplane initWithNumSeats:150 crew:5];

• The 1st line allocates storage and returns thememory address (which is stored in an Airplane*)

• The 2nd line tells plane to init itself. This is themethod that will actually set up the object.

Tuesday, January 17, 12

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 10/15

Making an Object In Less Space

• Can combine the previous two lines.

• Since [Airplane alloc] returns an object, we

can send it an init method immediately,rather than store it in a variable first:

• Airplane * airplane = [Airplane alloc];

[airplane init];

• Airplane * airplane = [[Airplane alloc]init];

Alloc returns a Airplane object, and we can send Airplane an init message

Tuesday, January 17, 12

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 11/15

id -- the most generic

t e• id plane = [[Airplane alloc]init];

• id can store a reference to any object

• id is almost the same as NSObject *

• Not exactly, however: a few classes in ObjC do notsubclass NSObject

• Because id is so generic, you can send it anything 

• [plane orderMeAPizza]; // no warnings at all!

• Bad idea, in general: only use it in init and whenappropriate

Tuesday, January 17, 12

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 12/15

nil

• Objective-C uses nil, a macro to 0, to indicate noobject

• Can send a message to nil: the message is simply

ignored (no crash, etc.)• NSString * message; // pointing to garbage

• int result = [message intValue]; // crash!

• NSString * message2 = nil; // safer way to handle this• int result = [message2 intValue]; // OK

Tuesday, January 17, 12

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 13/15

Scope / Memory

Management Issues• void doSomething(){

• double radius = 0.0; // on stack, goes out of scope

• }

• void doSomethingElse(){

• Airplane * plane = [[Airplane alloc]init];

• // Do something with plane

• }

• Q: What will happen to plane?

• A: Memory leak 

Tuesday, January 17, 12

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 14/15

Scope/Memory

Mana ement II• void doSomethingElse(){

• Airplane * plane = [[Airplane alloc]init];

• // Do something with the plane

• [plane release];

• }

• If you alloc, you must eventually release

Tuesday, January 17, 12

8/3/2019 Ch 2 & 3 - ObjC Objects and Messages

http://slidepdf.com/reader/full/ch-2-3-objc-objects-and-messages 15/15

References

• Neuburg, Matt. Programming iOS 4. O’ReillyMedia, Inc, 2011.

T d J 17 12