In Objective-C, the rule that the designated initializer is always invoked is not always executed?

Can we rely on the fact that in Objective-C the rule is that the initializer assigned by the class is always called exactly? Or can we say that this should almost always be true, with the exception of a few exceptions? For example, for UIView docs it says:

initWithFrame:

If you create a view object programmatically, this method is the designated initializer for the UIView class. Subclasses can override this method to perform any custom initialization, but should call super at the beginning of their implementation.

If you use Interface Builder to develop your interface, this method is not called when your view objects are then loaded from the nib file. Objects in the file bank are restored and then initialized using initWithCoder: Method

Or we can say that if it is programmatic, the rule should always apply for well-designed classes, but the Builder interface is a little different because it kind of β€œanimates” or builds an object in a non-programmatic way. If so, are they any other exceptions when programming iOS?

+4
source share
1 answer

The fact is that the class created using Interface Builder is unpacked and not initialized.

Archiving implies that the class is not initialized, but not archived; therefore, the initWithCoder: assumes responsibility for configuring the control when loading it using the archive attributes configured by Interface Builder.

You must put your initialization operations in the awakeFromNib: method, which is called in each case after loading the object, so you will be sure that your initialization statements will be called.

+4
source

Source: https://habr.com/ru/post/1435623/


All Articles