What is ItemsViewController's designated initializer?


#1

Hi All,

In chapter 2 we learn that:

[ul]
A class inherits all initializers from its superclass and can add as many as it wants for its own purposes.
Each class picks one initializer as its designated initializer.
Any other initializer a class has calls the class’s designated initializer.
If a class declares a designated initializer that is different from its superclass, the superclass’s designated initializer must be overridden to call the new designated initializer.

[/ul]

In ItemsViewController, we have the init method overridden to call the superclass’s initWithStyle method, which is the designated initializer. The designated initWithStyle method is then overridden to call the init method. So based on the way the methods are chained, can we conclude that the designated initializer for ItemsViewController is “init”? Is this the (correct) intent?
If not should we chain the initializers this way instead:

- (id) init {
    return [self initWithStyle:UITableViewStyleGrouped]; //Calls the designated initializer. Doesn't matter actual value passed in; it will be ignored
}

- (id) initWithStyle:(UITableViewStyle)style {
    self = [super initWithStyle:UITableViewStyleGrouped]; // Calls the superclass's designated initializer
    
    if (self) {
        for (int i =0; i < 5; i++) {
            [[BNRItemStore sharedStore] createItem];
        }
        
    }
    
    return self;
}

I know rules are meant to be broken :smiley: , but I also like to be consistent in our approach. What do you guys think?


#2

init is the designated initializer, and initWithStyle: is overrode to call the new designated initializer.


#3

I got the same question.
Is there any hint to know which designated initializer is for the class?

I thought ‘initWithStyle’ is the designated initializer as ItemsViewController is the subclass of UITableViewController which designated initializer is ‘initWithStyle’.
Without your answer here, I think I would never know. :unamused:


#4

To figure out the designated initializer, you’ll typically have to read the “Summary” section in the documentation for the class. Sometimes they say, sometimes they don’t - but that really depends on the class. Typically, a class that is meant to be subclassed (UIViewController, NSObject) will mention the designated initializer.

When it comes to classes you write yourself, a handy comment always helps:

// THIS is the designated initializer
- (id)initWithFoo:(int)foo;
- (id)initWithBar:(int)bar;

And yeah, it would be nice if there were some way to specify at compile-time what the designated initializer is.


#5

In the “Subclassing UITableViewController” section of chapter 9, it states “The designated initializer of UITableViewController is initWithStyle:.” It never says that the designated initializer for ItemsViewController is init. A sentence stating that, would have made things clear, at least for me. I made the assumption that ItemsViewController’s designated initializer was also initWithStyle: and was confused by the implementation of init and initWithStyle:. Once you know that the designated initializer for ItemsViewController is init, the implementation of the two init methods makes sense.


#6

@stevet

From my understanding, all classes have an init method, any other methods of initializing build upon the init method by adding additional functionality on top of the init method. With this in mind, having all the functionality that is common across all init type methods in the init method itself is more efficient and easier to read.

Also, for the same reason listed above(that all classes have an init method), your line of code may run into issues when you try to do

You may not find a super class with initWithStyle implemented