Init methods in BNRItemsViewController

#1

Hey!

Is there a mistake in code. My xCode give me warning on these

- (instancetype)init
{
    // Call the superclass's designated initializer
    self = [super initWithStyle:UITableViewStylePlain];
    return self;
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [self init];
}

I guess it is better this way:

[code]- (instancetype) init
{

return [self initWithStyle:UITableViewStylePlain];

}

  • (instancetype)initWithStyle:(UITableViewStyle)style
    {

    self = [super initWithStyle:UITableViewStylePlain];
    if (self)
    {

      for (int i = 0; i < 5; i++)
      {
    
          [[BNRItemStore sharedStore] createItem];
    
      }
    

    }

    return self;

}
[/code]

Any thoughts anyone?

#2

What did the warnings say?

#3

Two warnings… On both lines of the initWithStyle: method as follows:

-(instancetype)initWithStyle:(UITableViewStyle)style //warning!> Designated initializer missing a 'super' call to a designated initializer of the super class { return [self init]; //warning!> Designated initializer should only invoke a designated initializer on 'super' }

Xcode 7.2

#4

To get rid of the warnings, write that initializer like this:

-(instancetype)initWithStyle:(UITableViewStyle)style
{
    return [super initWithStyle:style];
}
#5

One way to get rid of them, but your proposal (however right it may sound) doesn’t fit the “requirement” in the book stating that “either you call -init or -initWithStyle, the style of the tableView will always be ‘Plain’”.

And as not-as-nice-as-it-could-have-been, Atem’s proposal actually does init the tableViewController in ‘plain’ style whether you (or your team mates) call -init or -initWithStyle regardless.

Could be

-(instancetype)initWithStyle:(UITableViewStyle)style { return [super initWithStyle:UITableViewStylePlain]; }
But again, if any changes to the default style would be required, you would need to change it in more than one place. Not ideal.