3

I have read up quite a bit on designated and convenience initializers in objective-c and feel I have a good understanding of how and why they are used. I just have a couple of outstanding questions, well one really.

Say you have a class that inherits directly from NSObject and you create a designated initializer for it.

I realise you can now mark your designated initializer using NS_DESIGNATED_INITIALIZER but I am wondering if that means it forces you to separately, within the same class, first override init with a call to your designated initializers?

Secondly, if you weren't to use this macro, what might the implications be of NOT overriding init explicitly with the call to the designated initializer? I realise the compiler would not know which was the designated initializer, so could anything bad come of this, and is it just good practice to also override init when you create a new designated initalizer for your class?

EDIT: I just want to add further clarification on this question. I have seen examples of good coders not adding a distinct method to override init despite having added a designated initialiser of their own making. When I ask if you should override init I mean in addition to creating your own specific designated initialiser which of course should call its superclass's init method. I have seen a good coder not do this and so I wondered why you would therefore do it.

cheznead
  • 2,589
  • 7
  • 29
  • 50
  • Just to add as a comment. I think the reason why the coder I mentioned created their own designated initialiser and didn't also override init inside the class was because their custom designated initialiser was just calling NSObject's init through a call to super and therefore that particular init wasn't doing too much so they let it go. Just my take on it for now. – cheznead Nov 05 '15 at 18:49
  • I think the above is a bad idea though. I have consulted Matt Neuberg's iOS 7 Programming Fundamentals book and 'in it' on pg. 107 he says 'a class that defines a designated initialiser should also override the inherited designated initialiser (in this case, init). And you can see why: if we don't, someone could say [[Dog alloc] init]... and create a dog without a number - the very thing our initialiser is trying to prevent'. – cheznead Nov 09 '15 at 13:43

1 Answers1

3

you can now mark your designated initializer using NS_DESIGNATED_INITIALIZER but I am wondering if that means it forces you to separately, within the same class, first override init with a call to your designated initializer?

Calling any initializer in self will bypass the warning. When I try to call a super.init method I do get the following warning.

Convenience initializer missing a 'self' call to another initializer.

what might the implications be of NOT overriding init explicitly with the call to the designated initializer?

This would mean your object might be missing some important information that it requires to function, or that the app assumes the object always has, and if it doesn't have it, it could cause errors or a crash in your app.

Here is a good explanation of how it works:

http://timekl.com/blog/2014/12/09/objective-cs-designated-secret/

Alex
  • 3,861
  • 5
  • 28
  • 44