Retired Document
Important: This document may not represent best practices for current development. Links to downloads and other resources may no longer be valid.
Initialization
Initialization is the stage of object creation that makes a newly allocated object usable by setting its state to reasonable initial values. Initialization should always occur right after allocation. It is performed by an initializer method (or simply, an initializer), which you always invoke on a newly allocated object. Initializers can also perform other setup tasks that bring the object into a useful state, such as loading resources and allocating heap memory.
The Form of an Initializer Declaration
By convention, the name of an initializer always begins with init
. It returns a dynamically typed object (id
) or, if initialization does not succeed, nil
. An initializer can include one or more parameters that specify initial values.
Here is a sample declaration of an initializer from the NSString
class:
- (id)initWithData:(NSData *)data encoding:(NSStringEncoding)encoding |
Implementing an Initializer
A class generally implements an initializer for its objects, but is not required to. If a class does not implement an initializer, Cocoa calls the initializer of the nearest ancestor of the class. However, subclasses often define their own initializer or override an initializer of their superclass to add class-specific initializations. If a class does implement an initializer, it should invoke an initializer of its superclass as the first step. This requirement ensures a series of initializations for an object down the inheritance chain, starting with the root object. The NSObject
class declares the init
method as the default object initializer, so it is always invoked last but returns first.
The basic steps for implementing an initializer method are the following:
Invoke the superclass initializer and check the value it returns. (Use the reserved word
super
to designate the superclass.) If the value is notnil
, the superclass initializer has returned a valid object, so you may proceed with initialization.Assign values to the object’s instance variables. In memory-managed code, if those values are objects themselves, copy or retain them, as appropriate.
Return the initialized object or, if initialization did not succeed, return
nil
.
Here is a simple initializer that follows these steps, initializing its date
instance variable to the current date:
- (id)init { |
if (self = [super init]) { // equivalent to "self does not equal nil" |
date = [[NSDate date] retain]; |
} |
return self; |
} |
In this code, if the superclass returns nil
, the method skips initialization and returns that value to its caller.
A class may have multiple initializers. This occurs when the initialization data can take varied forms or where certain initializers, as a matter of convenience, supply default values. In this case, one of the initialization methods is called the designated initializer, which takes the full complement of initialization parameters.
Copyright © 2018 Apple Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2018-04-06