简体   繁体   中英

Creating Initializers in Objective-C

The previous instructions in the book that I am reading were to create a designated initializer for the BNRItem class, which the book walked me through:

BNRItem.h

// Designated initializer for BNRItem
- (instancetype)initWithItemName:(NSString *)name
                  valueInDollars:(int)value
                    serialNumber:(NSString *)sNumber;


BNRItem.m

- (instancetype)initWithItemName:(NSString *)name
                  valueInDollars:(int)value
                    serialNumber:(NSString *)sNumber {
    // Call the superclass's designated initializer
    self = [super init];

    // Did the superclass's designated initializer succeed?
    if (self) {
        // Give the instance variables initial values
        _itemName = name;
        _serialNumber = sNumber;
        _valueInDollars = value;

        // Set _dateCreated to the current date and time
        _dateCreated = [[NSDate alloc] init];
    }
    // Return the address of the newly created initialized object
    return self;
}

The book also explained the init method that gets inherited from BRNItem's superclass and how to override the method:

BNRItem.h

-(instancetype)init {
    return [self initWithItemName:@""];
}

Now, I am completing the challenge at the end of the chapter and I have a feeling that I am making this more complicated than it needs to be. The challenge reads:

Silver Challenge: Another Initializer

"Create another initializer method for the BNRItem class. This initializer is not the designated initializer of BNRItem. It takes an instance of NSString that identifies the itemName of the item and an instance of NSString that identifies the serialNumber."

Excerpt From: Joe Conway. “iOS Programming.” iBooks. https://itun.es/us/Zni-Wl

Below is the code that I have created:

BNRItem.h

// Another initializer
- (instancetype)initWithItemName:(NSString *)name
                    serialNumber:(NSString *)sNumber;


BNRItem.m

- (instancetype)initwithItemName:(NSString *)name
                    serialNumber:(NSString *)sNumber {
    return [self initwithItemName:name
                     serialNumber:@""];

Is my solution correct?

Please try this one, you can call the Designated initializer in your init method.

- (instancetype)initWithItemName:(NSString *)name
                    serialNumber:(NSString *)sNumber {
    // call the Designated initializer here
    return [self initWithItemName:name
                   valueInDollars:0 //It is a default value
                     serialNumber:sNumber];
}

We nominate a designated initializer to extend from the super-class, and all other intializers call this designated initializer. The purpose of a designated initializer is:

  • To ensure that a class is in after creation.
  • Avoid duplicating code. We have one intializer performing the config, and the others invoke this one by passing parameters.

If it makes sense we can have the designated initializer be private (by removing it from the header) and only expose simpler initializers. Eg when some of the parameters might be nil, rather than have the user guess if this is valid or not, we can provide an initializer with just the parameters for that usage.

All of your non-designated initializers will call the designated initializer. The designated initializer should expose the configurable parameters for all cases.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM