object-oriented programming with objective-c · 2010. 11. 2. · fusion of smalltalk and c ... •...

44
Object-Oriented Programming with Objective-C Lecture 2

Upload: others

Post on 26-Feb-2021

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Object-Oriented Programming with

Objective-CLecture 2

Page 2: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Objective-C

Page 3: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

A Little History• Originally designed in the 1980s as a

fusion of Smalltalk and C

• Popularized by NeXTSTEP in 1988 (hence the ubiquitous “NS”)

• Apple bought NeXT in 1996, acquiring their operating system along with Steve Jobs

Page 4: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

A Little History

1988 1996

1980s

Brad Cox & Tom Love

Page 5: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Features

• Simple extension to C

• Low level efficiency and interoperability

• Dynamic runtime, like Smalltalk

• Optional static typing

Page 6: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

“Object-Oriented”

Page 7: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

“I thought of objects being like biological cells and/or individual computers on a network, only able to communicate with messages”

– Alan Kay, one of the fathers of OOP

Page 8: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

• Procedural: functions operating on data

• Object-oriented: objects communicating with messages

Page 9: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Objects

Internal State

Met

hod Method

Method

Page 10: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Objects

Internal State

Met

hod Method

Method

Internal State

Met

hod Method

Method“Method”

Reply

Page 11: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

So what does Objective-C even

look like?

Page 12: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Objective-C Files

Header file Implementation file(public) (private)

Page 13: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Header file#import <Cocoa/Cocoa.h>

@interface MyImage : NSObject{ NSString *name; char pixels[16][16];}

- (id)initWithName:(NSString *)name;

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

- (char)getPixelAtX:(int)x y:(int)y;- (void)setPixel:(char)value atX:(int)x y:(int)y;

- (NSData *)imageData;

@end

MyImage.h

Page 14: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Header file#import <Cocoa/Cocoa.h>

@interface MyImage : NSObject{ NSString *name; char pixels[16][16];}

- (id)initWithName:(NSString *)name;

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

- (char)getPixelAtX:(int)x y:(int)y;- (void)setPixel:(char)value atX:(int)x y:(int)y;

- (NSData *)imageData;

@end

Import Cocoa libraries so we can use Cocoa classes MyImage.h

Page 15: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Header file#import <Cocoa/Cocoa.h>

@interface MyImage : NSObject{ NSString *name; char pixels[16][16];}

- (id)initWithName:(NSString *)name;

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

- (char)getPixelAtX:(int)x y:(int)y;- (void)setPixel:(char)value atX:(int)x y:(int)y;

- (NSData *)imageData;

@end

Create a new class called MyImage, a subclass of NSObject

MyImage.h

Page 16: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Header file#import <Cocoa/Cocoa.h>

@interface MyImage : NSObject{ NSString *name; char pixels[16][16];}

- (id)initWithName:(NSString *)name;

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

- (char)getPixelAtX:(int)x y:(int)y;- (void)setPixel:(char)value atX:(int)x y:(int)y;

- (NSData *)imageData;

@end

Define the instance variables for the MyImage class

MyImage.h

Page 17: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Header file#import <Cocoa/Cocoa.h>

@interface MyImage : NSObject{ NSString *name; char pixels[16][16];}

- (id)initWithName:(NSString *)name;

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

- (char)getPixelAtX:(int)x y:(int)y;- (void)setPixel:(char)value atX:(int)x y:(int)y;

- (NSData *)imageData;

@end

Declare the methods for the MyImage class

MyImage.h

Page 18: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Declaring a Method

- (char)getPixelAtX:(int)x y:(int)y;

Page 19: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Declaring a Method

- (char)getPixelAtX:(int)x y:(int)y;

The return type of the method — in other words, the type of the expression [obj getPixelAtX:x y:y]

Page 20: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Declaring a Method

- (char)getPixelAtX:(int)x y:(int)y;

The name of the method, also called its selector

Objective-C uses named parameters, a feature from Smalltalk which improves readability

Page 21: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Declaring a Method

- (char)getPixelAtX:(int)x y:(int)y;

Parameter variables with their types

Page 22: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Implementation file#import "MyImage.h"

@implementation MyImage

- (NSString *)name { return name;}

- (void)setPixel:(char)value atX:(int)x y:(int)y { if (x < 0 || y < 0 || x >= 16 || y >= 16) { [NSException raise:@"InvalidRangeException" format:@"Pixel location outside image range"]; } pixels[x][y] = value;}

// all the other methods

@end

MyImage.m

Page 23: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Implementation file#import "MyImage.h"

@implementation MyImage

- (NSString *)name { return name;}

- (void)setPixel:(char)value atX:(int)x y:(int)y { if (x < 0 || y < 0 || x >= 16 || y >= 16) { [NSException raise:@"InvalidRangeException" format:@"Pixel location outside image range"]; } pixels[x][y] = value;}

// all the other methods

@end

MyImage.m

Import the header file so we know what weʼre implementing

Page 24: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Implementation file#import "MyImage.h"

@implementation MyImage

- (NSString *)name { return name;}

- (void)setPixel:(char)value atX:(int)x y:(int)y { if (x < 0 || y < 0 || x >= 16 || y >= 16) { [NSException raise:@"InvalidRangeException" format:@"Pixel location outside image range"]; } pixels[x][y] = value;}

// all the other methods

@end

MyImage.mTell Obj-C we are defining methods for MyImage

Page 25: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Implementation file#import "MyImage.h"

@implementation MyImage

- (NSString *)name { return name;}

- (void)setPixel:(char)value atX:(int)x y:(int)y { if (x < 0 || y < 0 || x >= 16 || y >= 16) { [NSException raise:@"InvalidRangeException" format:@"Pixel location outside image range"]; } pixels[x][y] = value;}

// all the other methods

@end

MyImage.mDefine the methods using Objective-C code

Page 26: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Sending a Message

[object method];

Page 27: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Sending a Message

int age1 = [person age];int age2 = [[person father] age];

Page 28: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Sending a Message

[array addObject:obj];[array insertObject:obj atIndex:0];

Page 29: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

self and super

- (NSTimeInterval)age{ return [[NSDate date] timeIntervalSinceDate:[self birthday]];}

- (NSTimeInterval)age{ return [super age] + 60;}

self is a reference to the current object

super allows access to the superclassʼs methods

Page 30: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Class Methods

+ (BOOL)isSubclassOfClass:(Class)aClass;

The plus sign indicates that this is a class method, received by the class itself (instead of some particular instance of the class)

Page 31: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Class Methods

+ (BOOL)isSubclassOfClass:(Class)aClass;

Class methods are called on the class itself

[NSButton isSubclassOfClass:[NSControl class]]

Page 32: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Variables• Variables are declared just like in C:

int i = 0, j;float x = 1.0;NSString *str = @"asdf";

• Objects are always pointers (NSString *)

• Instead of "C strings", Cocoa uses @"NSStrings", which can contain Unicode text

Page 33: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Objective-C Types• Dynamically-typed:

id fido;

• Statically-typed:MyDog *fido;

• Objective-C uses dynamic binding; method names are not resolved until run time

Page 34: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

nil• nil is the “nothing object”

• If person == nil then:[person age] == 0[person name] == nil[person eat:taco] // fails silently

• Some methods take nil as a “donʼt care” parameter

Page 35: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

BOOL

• Objective-C was developed before C99, when C gained a boolean type

YEStrue1TRUE

NOfalse0FALSE

ObjCC++ / C99

actual valueC

Page 36: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Class• Classes are (almost) like any other object

• Type is Class , no *

Class myClass = [MyClass class];[myClass classMethod];if ([someObject isKindOfClass:myClass]) // the object is a MyClass

Page 37: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Selectors

SEL fetchSelector = @selector(fetch:);[fido performSelector:fetchSelector withObject:theBall];

Includes all colons

is equivalent to

[fido fetch:theBall];

but you can store SELs and pass them around(you can even call NSSelectorFromString(NSString *aSelectorName)!)

Page 38: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Selectors in Cocoa

if ([obj respondsToSelector:@selector(fetch:)]) { [obj fetch:theBall];}

Delegates / “duck typing”

[button setTarget:self];[button setAction:@selector(buttonPressed:)];

Target / action pattern

Page 39: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Cocoa Foundations

Page 40: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

The Life of an Object

• Allocation

• Initialization

• …

• Deallocation

Page 41: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

The Life of an Object+ (id)alloc; - (id)init;

MyClass *myInstance = [[MyClass alloc] init];

Page 42: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

The Life of an Object- (void)dealloc;

This is called by Cocoa — never call dealloc directly!

Page 43: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

The Life of an Object• Your objects should have:

• an init method which initializes all instance variables

• a dealloc method which cleans up any resources you have allocated

• We will discuss this in more depth when we cover memory management

Page 44: Object-Oriented Programming with Objective-C · 2010. 11. 2. · fusion of Smalltalk and C ... • Object-oriented: objects communicating with messages. Objects Internal State Method

Naming Conventions• Variables start with a lowercase letter

and use capitalization to distinguish words: e.g. thisIsAGoodName, but not a_bad_name

• Class names start with a prefix indicating the project, company, or author to avoid name collisions: e.g. SRScroller or ProjMainView, but not mutableString or TEXT_FIELD