November 2013
Beginner
325 pages
9h 47m
English
In some cases, an initial value of zero for your instance variables may work fine. In others, however, you will need instances of your class to come into the world with their instance variables initialized to non-zero values.
Let’s say that every instance of BNRAppliance should start its life with a voltage of 120. In BNRAppliance.m, override NSObject’s init method by adding a new implementation of init.
- (instancetype)init
{
// Call the NSObject's init method
self = [super init];
// Did it return something non-nil?
if (self) {
// Give voltage a starting value
_voltage = 120;
}
// Return a pointer to the new object
return self;
}
Now when you create a new instance of BNRAppliance, it will have a voltage of 120 ...
Read now
Unlock full access