You have a perfectly good initializer for BNRAppliance, but let’s take a moment to look at a variation that you will see in other people’s code. We typically do a plain assignments in an initializer, but many programmers will use the accessor methods. Change initWithProductName: to do this:
- (instancetype)initWithProductName:(NSString *)pn { // Call NSObject's init method self = [super init]; // Did it return something non-nil? if (self) { // Set the product name [self setProductName:pn]; // Give voltage a starting value [self setVoltage:120]; } return self; }
In most cases, there is little reason to do one over the other, but it makes for a great argument. The argument goes like this: The assign guy says, “You cannot use an accessor method in an init method! The accessor assumes that the object is ready for work, and it is not ready for work until after the init method is complete.” Then the accessor method guy says, “Oh, come on. In the real world that is almost never an issue. My accessor method might be taking care of other stuff for me. I use my accessor anytime I set that variable.”
Either approach will work in the vast majority of cases. Build and run the program.
At Big Nerd Ranch, we tend to set the instance variables directly and we typically do the assignment and check for the superclass’s initializer in one line. Do this in your initWithProductName: method:
- (instancetype)initWithProductName:(NSString *)pn { if (self = [super init]) { _productName = [pn copy]; _voltage = 120; } return self; }