0

I am reading a book which teaches that we should implement a designated initializer of class, and then let other initializers call this one (which I agree with).

Now, I am in a section in this book, which says: "The designated initializer of UITableViewController is initWithStyle:". And then goes in the implementation like this:

// inside ItemsViewController.m, subclass of UITableViewController

-(id) init{
    // Call the superclass's designated initializer
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {

    }
    return self;
}

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

After this code, the book also says: "This will ensure that all instances of ItemsViewController use the UITableViewStyleGrouped style, no matter what initialization message is sent to it."

To stick to the principle I described in the beginning of the post, I might had implemented this class like this:

// inside ItemsViewController.m, subclass of UITableViewController


// Implement the designated initializer first
-(id) initWithStyle:(UITableViewStyle)style
    // Call the superclass's designated initializer
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {

    }
    return self;
}

// Call designated initializer
- (id)init
{
    return [self initWithStyle: nil];
}

But I think I am missing something? I think the book uses init as designated initializer in their case?

2
  • basically your code and the code in the book do the same thing. there are different types of initializers you use the one you want.
    – Radu
    Jul 29, 2013 at 9:16
  • If you don't want anyone to change the style of the cell, it's better to override the initWithStyle: and throw an exception there, e.g. NSAssert(NO, @"You are not supposed to call this method. Use init instead");
    – Sulthan
    Jul 29, 2013 at 9:47

3 Answers 3

0

Construction

// Implement the designated initializer first
-(id) initWithStyle:(UITableViewStyle)style
    // Call the superclass's designated initializer
    self = [super initWithStyle:UITableViewStyleGrouped];
    if (self) {

    }
    return self; }

is confusing for user of class, because he will think that you provided initWithStyle constructor to allow him setting controllers' style. You should use your first init method.

0

If you have custom tableviewcell class then you dont need to implement initwithstyle initializer and also that initialilzer will not be called for primitive and class member objects.you will have to suffer.so follow the standard initialiser.

0

Basically your code and the code in the book do the same thing. there are different types of initializers for classes, you use the one you need. What the example in the book wants to show you is how to overwrite them if you need some custom initializers later on. Sow it's understandable that it doesn't really make sense from a real live application point of view but from a learning overwriting initializers and customization i think it's ok.

2
  • so far what I can tell also is I think both my code and the code in the book are more or less same right? Jul 29, 2013 at 9:49
  • Yes they are the same there is no difference regarding performence the only difference is implementation.
    – Radu
    Jul 29, 2013 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.