![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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/1.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/2.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/3.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/4.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/5.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/6.jpg)
“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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/7.jpg)
“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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/8.jpg)
• 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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/9.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/10.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/11.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/12.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/13.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/14.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/15.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/16.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/17.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/18.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/19.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/20.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/21.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/22.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/23.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/24.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/25.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/26.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/27.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/28.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/29.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/30.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/31.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/32.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/33.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/34.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/35.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/36.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/37.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/38.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/39.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/40.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/41.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/42.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/43.jpg)
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](https://reader036.vdocuments.us/reader036/viewer/2022071405/60fadcab5571a32861635225/html5/thumbnails/44.jpg)
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