Upload
lisa-ridley
View
11.741
Download
2
Tags:
Embed Size (px)
DESCRIPTION
Objective-C: Object Initialization and Utility Methods
Citation preview
Objective-C: Objects– Initialization and Utility Methods
TN Valley Apple DevelopersSaturday CodeJamOctober 23, 2010
Saturday, October 23, 2010
Creating Objects: A Review
Objects are created in a two-step process: allocation (alloc) and initialization (init)
Allocation sets aside memory for the object; initialization sets base values for the object variables
Saturday, October 23, 2010
Init Method InheritanceAll Objective-C classes inherit ultimately from NSObject
As a result, all Objective-C classes have some common initialization methods
Initialization methods can be customized for your declared class by overriding inherited methods, or writing new initialization methods
Saturday, October 23, 2010
Return Type of InitInitialization methods should be typed to return an id rather than a pointer to a specific instance of the class
an init method may release its receiver and substitute an object belonging to a subclass (very common with class clusters)
typing the return value as id allows the init method to be inherited and used by child classes of the original class
Saturday, October 23, 2010
The Form of init
- (id) init {
if (self = [super init]) {
//perform class specific init tasks
}
return self;
}
Saturday, October 23, 2010
Naming Init Methods
When declaring custom init methods for your class, the universal naming convention is to begin the method name with “init”, i.e. initWithString, initWithFloat, etc.
Saturday, October 23, 2010
Initializers with ArgumentsInitializers that take one or more arguments can be used to initialize your class’s instance variables to a specific value
If not set to a specific value, all instance variables are set to zero or nil (depending on their type) by the alloc method when the object is created.
Saturday, October 23, 2010
Designated InitializerClasses can have multiple initialization methods that will initialize the instance variables in different ways
A class can, however, have only one “designated initializer”
The designated initializer is the initialization method that completely initializes an instance of the class
Saturday, October 23, 2010
Designated InitializerThe designated initializer is usually the initializer that:
has the most arguments, or
does the most work in setting up the object
For classes that don’t require initializers with arguments, the designated initializer is init
Saturday, October 23, 2010
Rules for Designing Initializers
A class’s designated initializer must invoke the designated initializer of its superclass
All other class initializers must eventually invoke the class’s designated initializer
Saturday, October 23, 2010
Example: TShirt Class
The TShirt class is a subclass of NSObject
Each instance of the TShirt class needs to have default values assigned to its instance variables to initialize it properly
Saturday, October 23, 2010
Example: Class TShirt
The TShirt class has two instance variables:
shirtSize, of type NSString, which gets initialized to a default value of “medium”
shirtColor, of type NSString, which gets initialized to a default value of “white”
Saturday, October 23, 2010
Example: TShirt ClassThe TShirt class will inherit the init method from NSObject
The designated initializer for TShirt will need to set the default values for the instance variables shirtColor and shirtSize, and call the designated initializer for its superclass, NSObject
Saturday, October 23, 2010
Example: TShirt Class
To make sure that any subclasses of the TShirt class are initialized properly, the TShirt class will need to override the init method inherited from NSObject in such a manner that the designated initializer for TShirt gets called
Saturday, October 23, 2010
Example: TShirt ClassCreate a new XCode Project
Command Line Tool of type Foundation, called “TShirt Factory”
Create a new Group called “Classes”
Create a new File in the Classes group
Cocoa Objective-C Class, subclass of NSObject, called TShirt (create header file too)
Saturday, October 23, 2010
Example: Class TShirt#import <Cocoa/Cocoa.h>
@interface TShirt : NSObject {! NSString *shirtSize;! NSString *shirtColor;}
@property (retain) NSString *shirtSize;@property (retain) NSString *shirtColor;
// The Designated Initializer of the TShirt class- (id) initWithShirtSize: (NSString *)size andColor: (NSString *)color;
@end
Saturday, October 23, 2010
Example: Class TShirt#import "TShirt.h"
@implementation TShirt
@synthesize shirtSize, shirtColor;
// Overriding the inherited init class- (id) init {! NSString *defaultShirtSize = @"medium";! NSString *defaultShirtColor = @"white";! // invoking the designated initializer, and returning the results! return [self initWithShirtSize:defaultShirtSize andColor:defaultShirtColor];!}
// The Designated Initializer of the TShirt class- (id) initWithShirtSize: (NSString *)size andColor: (NSString *)color { // Calls the designated initializer of the parent class! [super init];! // initializes the size and color of the TShirt object! [self setShirtSize:size];! [self setShirtColor:color];! return self;}
@end
Saturday, October 23, 2010
Example: TShirt Class
Open TShirt Factory.m, remove the NSLog statement and add the following:
! TShirt *myTShirt = [[TShirt alloc] init];! NSLog(@"myTShirt is initialized");! NSLog(@"myTShirt is size: %@",[myTShirt shirtSize]);! NSLog(@"myTShirt is color: %@",[myTShirt shirtColor]); ! [myTShirt release];
Save and Run, and open the Console to see the output of the log statements
Saturday, October 23, 2010
Example: TShirt Class
You should see something similar to the following:
2010-10-23 00:36:24.320 TShirts[54201:a0f] myTShirt is initialized2010-10-23 00:36:24.322 TShirts[54201:a0f] myTShirt is size: medium2010-10-23 00:36:24.323 TShirts[54201:a0f] myTShirt is color: white
Saturday, October 23, 2010
Example: TShirt ClassThe designated initializer, initWithShirtSize:andColor:, invokes the designated initializer of the superclass, sets the initial values of the instance variables shirtSize and shirtColor, and returns the initialized instance of the TShirt class
Saturday, October 23, 2010
Example: TShirt ClassThe inherited init class has been overridden to invoke the designated initializer, passing the default values of “medium” and “white” for the shirt size and color
The result is that, if not specified otherwise, an instance of the TShirt class will default to a shirtSize medium and shirtColor of white
Saturday, October 23, 2010
Utility Class MethodsThe primary function of a class is to create instances of the class (objects)
This is achieved with the methods alloc and init
Classes can also have other methods that create instances of the class, called Utility methods
Also referred to as “factory methods” or “Convenience constructors”
Saturday, October 23, 2010
Utility Class MethodsUtility Class methods combine allocation and initialization of class instances in a single method
Objects created with Utility Class methods are autoreleased – you do not own them, and therefore are not responsible for releasing them
You can specifically retain an object created with a utility class methods; if you do you own the object and are responsible for releasing that object when you no longer need it
Saturday, October 23, 2010
Identifying Utility Class Methods
Utility class methods are class methods, which will be preceded in code by a (+) instead of a (-), indicating that you don’t have to create an instance of the class to use them, and will (usually) have a return value of type id
Saturday, October 23, 2010
Calling Utility Class methodsYou can call a utility class method using the class name, followed by the method name and any parameters needed, and assigning that to a variable of the same type as the utility method’s class
MyClass *myvar = [MyClass utilityMethod];
Saturday, October 23, 2010
Some Familiar Utility Classes
NSString’s stringWithFormat:
NSString’s stringWithString:
NSNumber’s numberWithFloat:
NSNumber’s numberWithInt:
Saturday, October 23, 2010
Example: TShirt Utility MethodAn example of a utility method for our TShirt class might be one that returns a “large blue” Tshirt object
We’ll call this method largeBlueShirts
Add the following method to the TShirt initialization and implementation files
Saturday, October 23, 2010
Example: TShirt Utility Method@interface TShirt: NSObject{ ... }
...+ (id) largeBlueShirt;
@end
@implementation TShirt...+ (id) largeBlueShirt {! NSString *defaultShirtSize = @"large";! NSString *defaultShirtColor = @"blue";! // assign results from designated initializer to an autoreleased object! TShirt *shirt = [[[TShirt alloc ] initWithShirtSize:defaultShirtSize andColor:defaultShirtColor] autorelease];! // return the newly created TShirt object! return shirt;}
@end
Saturday, October 23, 2010
Example: TShirt Utility Method
Open TShirt Factory.m, and add the following lines after [myTShirt release]:
TShirt *myBlueTShirt = [TShirt largeBlueShirt];!! NSLog(@"myBlueTShirt is initialized");! NSLog(@"myBlueTShirt is size: %@",[myBlueTShirt shirtSize]);! NSLog(@"myBlueTShirt is color: %@",[myBlueTShirt shirtColor]);
Save and run, and pull up the Console
Saturday, October 23, 2010
Example: TShirt Utility Method
You should see something similar to the following:2010-10-23 00:36:24.320 TShirts[54201:a0f] myTShirt is initialized2010-10-23 00:36:24.322 TShirts[54201:a0f] myTShirt is size: medium2010-10-23 00:36:24.323 TShirts[54201:a0f] myTShirt is color: white2010-10-23 00:36:24.324 TShirts[54201:a0f] myBlueTShirt is initialized2010-10-23 00:36:24.324 TShirts[54201:a0f] myBlueTShirt is size: large2010-10-23 00:36:24.324 TShirts[54201:a0f] myBlueTShirt is color: blue
Saturday, October 23, 2010
When to use Utility MethodsIf you only need an instance of the class inside the current event loop and inside the current code block, you should consider using a utility method
If you need an instance of the class to persist outside the current code block or event loop, you should create the object using [[alloc]init], and release it in the dealloc method
Saturday, October 23, 2010
Our Next Meeting: Nov 20thChapter 8: Collecting Information
Common Cocoa Foundation Classes:
NSString
NSArray
NSDictionary
NSSet
NSNumber
NSNull
NSData
NSURL
Mutable vs. Immutable objects
Saturday, October 23, 2010