標籤:
Objective -C Object initialization 對象初始化
1.1 Allocating Objects 指派至
Allocation is the process by which a new object is born.
allocation 是新對象誕生的過程。
Sending the alloc message to a class causes that class to allocate a chunk of memory large enough to hold all its instance variables.
發送alloc 訊息能夠讓class分配足夠多得記憶體。
All your BOOLs start out as NO;
all your ints are 0; all your floats become 0.0; all your pointers are nil;
A newly allocated object isn‘t ready to be used right away: you need to initialize it before you can work with it.
一個剛分配的對象還不能用,你需要初始化它。
Some languages, including C++ and Java, perform object allocation and initialization in a single operation using a constructor. Objective-C splits the two operations into explicit allocation and initialization stages.
java和C++執行allocation and initialization 在一個操作裡。
而Objective-C分開了兩個操作。
1.2 Initializing objects 對象初始化
Initialization takes a chunk of memory and gets it ready to become a productive member of society. init methods—that is, methods that do initialization—almost always return the object they‘re initializing.
初始化讓其成為對象的一員。init methods 也就是說總是返回要被建立的對象。
Car *car = [[Car alloc] init];
1.3 Writing initializationg methods 寫初始化方法
(id) init {
if (self = [super init])
{
engine = [Engine new];
tires[0] = [Tire new];
tires[1] = [Tire new];
tires[2] = [Tire new];
tires[3] = [Tire new];
}
return (self);
} // init
The first bit of code that runs in that statement is [super init].That code lets the superclass do its initialization work.
[super init]允許父類完成初始化工作。
For classes that inherit from NSObject, calling on the superclass lets NSObject do any processing it needs to do so that objects can respond to messages and deal with retain counts.
對於任何繼承自NSObject的類,初始化能夠讓對象相應詳細和處理retain counts .
For classes that inherit from another class, this is their chance to do their own version of clean-slate initialization.
對於繼承自其他類的類,它有機會初始化自身。
Remember that instance variables are found at a memory location that‘s a fixed distance from the hidden self parameter.
記住在執行個體變數發現在記憶體的位置,固定位置能夠找到隱藏的self參數。
If a new object is returned from an init method, we need to update self so that any subsequent instance variable references affect the right places in memory.
如果一個對象返回一個init 方法,我們需要更新self 才能在任何子變數中適當的位置找到執行個體變數。
An init method can return nil if there‘s a problem initializing an object.
一個初始化方法可能返回nil 。
Keep in mind that this assignment affects the value of self only for this method. It doesn‘t change anything outside the method‘s scope.
記住這個分配隻影響self 在本方法內。不會影響其他方法。
The test if (self = [super init]) won‘t run the body code if nil is returned from [super init].
這個測試不會運行,如果[super init ]返回為nil的話。
An init method returns the object that was just initialized. Since we assigned the return value of [super init] to self, that‘s what we should return.
一個正確地初始化方法返回一個初始化對象。因為我們把[super init ]賦值給self ,所以返回self
1.4 What to Do When You‘re Initializing
This is the place to do your clean-slate initialization work. You assign values to instance variables and create the other objects that your object needs to do its work.
在這裡,你可以做一些從頭再來的工作。你分配值和建立你需要的其他對象.初始化需要做什麼工作還是看你自己的需求。
1.5 Convenience initializers
In fact, it‘s important to remember that init methods are nothing special. They‘re just ordinary methods that follow a naming convention.
init methods沒什麼特殊的。他們也是普通的類。
Many classes have convenience initializers. These are init methods that do some extra work, saving you the trouble of doing it yourself.
許多類提供簡單方法。做額外的工作。
- (id) initWithFormat: (NSString *) format, ...;
這個也是初始化方法。
Objective -C Object initialization 對象初始化