Ok, this is admittedly a slightly better question, but I want everything to be correct, so hopefully someone can enlighten me:
The script is pretty standard, but it has one twist: I have a class in the structure that I am writing that inherits directly from NSObject . It has a designated initializer with many arguments, most of which are nonnull . Since this is part of the structure, I explicitly use the NS_DESIGNATED_INITIALIZER macro (which I do not always do in small personal applications). The problem is that this causes Xcode to warn me of overriding init , that is, the initializer assigned by the superclass. But, in addition, it requires me to call my designated initalizer, which I cannot do, because I simply do not have enough meaningful default values ββfor its arguments. I really do not want to throw an exception in the "small" init , I would really like to return nil .
To get rid of the warning, I added init as the second designated initializer in my class extension, for example:
@interface MyClassName () // some other stuff not relevant` -(nullable instancetype)init NS_DESIGNATED_INITIALIZER; @end
Now I can safely just return nil; in the overriden init method as I wanted. This means that my documentation (I use appledoc) and the Xcode extension of the extension code will not tell someone using my framework that init is actually also a designated initializer (so they will not accidentally use it), but it is still there (for example, in unit tests this may come in handy).
My question is: are there any dangers for this, besides the fact that someone really uses it in production, joyfully sending messages to zero after that, not understanding? Will this be one of the few cases where it is preferable to throw an exception in init?
source share