Doesn't [super init] in the init method ever call correctly?

In Microsoft WinObjC UIApplication.mm file ( https://github.com/Microsoft/WinObjC/blob/master/Frameworks/UIKit/UIApplication.mm ) method init

is implemented for both UIApplication

and for WOCDisplayMode

.

Neither the class method init

calls [super init]

, nor any method from this family of methods that will ultimately result in the call [super init]

. I've never seen this before, other than initializing objects NSProxy

.

I have reproduced the implementation at the time of writing WOCDisplayMode

below for reference.

-(instancetype) init
{
    _fixedWidth = 320.0f;
    _fixedHeight = 480.0f;
    _fixedAspectRatio = 0.0f;
    _magnification = 1.0f;
    _autoMagnification = TRUE;
    _sizeUIWindowToFit = TRUE;
    _operationMode = WOCOperationModePhone;
    return self;
}

      

It seems to me that this can create a number of problems; for example, if one of the superclasses UIApplication

, for example UIResponder

, at some point overrides init

and configures the internal state on which future method calls depend.

Why did the developer decide not to name it [super init]

? Is this ever a worthwhile decision? Is it correct?

+3


source to share


1 answer


This definitely seems like a mistake on behalf of the author (s) of these classes.

NSProxy

does not call [super init]

because it is an abstract superclass and does not inherit from NSObject

.

Since their implementation UIApplication

inherits from UIResponder

and WOCDisplayMode

inherits from NSObject

, they must call [super init]

in these classes.



According to the documentation on Object Initialization :

The requirement to call the superclass initializer as the first action is important. Recall that an object not only encapsulates the instance variables defined by its class, but the instance variables defined by all of its parent classes. Calling the initializer first, you help ensure that the instance variables defined by the classes up the inheritance chain are initialized first. The immediate superclass, in its initializer, calls the initializer of its superclass, which calls the main init method ... of its superclass, etc. (see Figure 6-1). Correct initialization order because later initializations of subclasses may depend on instance variables defined by the superclass being initialized to reasonable values.

I would recommend registering it as a problem in the project.

+5


source







All Articles