Object Oriented Programming The Objective C Language Foundation Classes
Object Oriented Programming The Objective C Language Foundation Classes
Lecture 2
■ http://developer.apple.com
■ Dev site uses Google search
Objects
OOP Vocabulary
• Class: defines the grouping of data and code,
the “type” of an object
• Instance: a specific allocation of a class
• Method: a “function” that an object knows how to perform
• Instance Variable (or “ivar”): a specific piece of data
belonging to an object
OOP Vocabulary
• Encapsulation
■ keep implementation private and separate from interface
• Polymorphism
■ different objects, same interface
• Inheritance
■ hierarchical organization, share code, customize or extend
behaviors
Inheritance
Memory
Superclass
NSObject management
Generic
NSControl behaviors
Specific
NSButton NSTextField behaviors
Subclass
// method declarations
- (NSString *)name;
- (void)setName:(NSString *)value;
- (int)age;
- (void)setAge:(int)age;
- (BOOL)canLegallyVote;
// alternative setter
- (void)setName:(NSString *)name age:(int)age;
@end
Class interface declared in header file
#import <Cocoa/Cocoa.h>
// method declarations
- (NSString *)name;
- (void)setName:(NSString *)value;
- (int)age;
- (void)setAge:(int)age;
- (BOOL)canLegallyVote;
@end
Class interface declared in header file
#import <Cocoa/Cocoa.h>
// method declarations
- (NSString *)name;
- (void)setName:(NSString *)value;
- (int)age;
- (void)setAge:(int)age;
- (BOOL)canLegallyVote;
@end
Class interface declared in header file
#import <Cocoa/Cocoa.h>
// method declarations
- (NSString *)name;
- (void)setName:(NSString *)value;
- (int)age;
- (void)setAge:(int)age;
- (BOOL)canLegallyVote;
@end
Class interface declared in header file
#import <Cocoa/Cocoa.h>
// method declarations
- (NSString *)name;
- (void)setName:(NSString *)value;
- (int)age;
- (void)setAge:(int)age;
- (BOOL)canLegallyVote;
@end
Defining a class
A public header and a private implementation
@implementation Person
// method implementations
- (int)age {
return age;
}
- (void)setAge:(int)value {
age = value;
}
- (BOOL)canLegallyVote {
return (age > 17);
}
// and others as declared in header...
@end
Private implementation defines methods
#import "Person.h"
@implementation Person
// method implementations
- (int)age {
return age;
}
- (void)setAge:(int)value {
age = value;
}
- (BOOL)canLegallyVote {
return (age > 17);
}
// and others as declared in header...
@end
Private implementation defines methods
#import "Person.h"
@implementation Person
// method implementations
- (int)age {
return age;
}
- (void)setAge:(int)value {
age = value;
}
- (BOOL)canLegallyVote {
return (age > 17);
}
// and others as declared in header...
@end
self and super
Methods have an implicit local variable named “self”
(like “this” in C++)
- (void)doSomething {
[self doSomethingElseFirst];
...
}
Also have access to methods of the superclass using super
- (void)doSomething {
[super doSomething];
...
}
Messaging syntax
Message examples
Person *voter; //assume this exists
[voter castBallot];
[voter setAge:21];
if ([voter canLegallyVote]) {
// do something voter-y
}
• Statically-typed object
Person *anObject
■ Object equality
NSString
• General-purpose Unicode string support
■ Unicode is a coding system which represents all of the world’s
languages
• Consistently used throughout Cocoa instead of “char *”
• Without doubt the most commonly used class
• Easy to support any language in the world with Cocoa
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!”;
Format Strings
• Similar to printf, but with %@ added for objects
NSString *log = [NSString stringWithFormat: @”It’s ‘%@’”, aString];
• Example:
NSString *myString = @”Hello”;
NSString *fullString;
fullString = [myString stringByAppendingString:@” world!”];
NSString
• Common NSString methods
- (BOOL)isEqualToString:(NSString *)string;
- (BOOL)hasPrefix:(NSString *)string;
- (int)intValue;
- (double)doubleValue;
• Example:
NSString *myString = @”Hello”;
NSString *otherString = @”449”;
if ([myString hasPrefix:@”He”]) {
// will make it here
}
if ([otherString intValue] > 500) {
// won’t make it here
}
NSMutableString
• NSMutableString subclasses NSString
• Allows a string to be modified
• Common NSMutableString methods
- (void)appendString:(NSString *)string;
- (void)appendFormat:(NSString *)format, ...;
+ (id)string;
NSEnumerator *e;
id object;
e = [someCollection objectEnumerator];
while ((object = [e nextObject]) != nil) {
...
}
NSNumber
• In Objective-C, you typically use standard C number types
• NSNumber is used to wrap C number types as objects
• Subclass of NSValue
• No mutable equivalent!
• Common NSNumber methods
+ (NSNumber *)numberWithInt:(int)value;
+ (NSNumber *)numberWithDouble:(double)value;
- (int)intValue;
- (double)doubleValue;
Other Classes
• NSData / NSMutableData
■ Arbitrary sets of bytes
• NSDate / NSCalendarDate
■ Times and dates
• NSAttributedString
■ Basis of the Cocoa rich text system
■ Attributes are fonts, colors, etc.
Getting some objects
• Until we talk next time:
■ Use class factory methods
■ NSString’s +stringWithFormat:
■ NSArray’s +array
■ NSDictionary’s +dictionary