Objective-c init

Should init be called after selecting an object? I mean: in Java, for example, if you do not name it (constructor), it will be called anyway (default constructor). In Objective-C, you can create an alternative constructor (like Java), and one of the most interesting things is self = [super init] . I read: an article about a coconut chapter , but in the end itโ€™s not clear why we should make this assignment self = [super init] . It just says that [super init] can return another object, and then we need to replace self with this new object. This does not explain why we do this in the first place.

+6
source share
3 answers

Does init really need to be called after the object is selected?

Yes, it is necessary. For comparison with Java, calling [super init] (or another designated initializer) effectively starts the superclass constructor. This mechanism is provided to you in Java, but not in Objective-C. Therefore, it is not called implicitly in ObjC, as in Java. In ObjC, you must explicitly invoke one of the designated superclass initializers. If you do not invoke your superclass initializer, your object will not be fully initialized. This can result in no side effects, or it can lead to a completely unusable object that causes undefined behavior.

why should we make such an assignment self = [super init].

To the right, alloc creates a selection (with zero memory) and sets a pointer to the information of the isa class, but the superclass initializer allows the superclass to exchange an instance with another, which may be more appropriate. As a rule, you avoid doing this in your subclasses. Another reason for doing this and checking for nil is because it is an error handling tool. In ObjC, exceptions are generally not recoverable, so the usual way to report an error to a subclass is to return nil . That is why it is also important not only to assign self , but also to check it against nil .

+5
source

There is a big difference between Objective-C and Java. Java is an interpreted language built from the ground up, and Objective-C is on top of C. Everything you can do in C also works in Objective-C.

Unlike Java, C (and by extension Objective-C) has a "raw memory allocation". In Java, calling new automatically calls the constructor. Language does not allow to bypass this mechanism. However, in Objective-C, you can allocate raw memory. The memory does not contain a ready-to-use object at the time of its return from alloc - it is only partially prepared for use. The memory block has a reference counter, and it provides enough space to place your object, but it is not ready to receive messages that have been implemented by your own subclass. This is why you should call init (or use new , which combines alloc and init for you).

Assigning / checking if (self = [super init]) allows you to trap errors during the construction phase of your facility. If your own initializer fails, you can set self = nil to report the problem to the chain. Assigning another object is much less common, but it can also be done.

+3
source

After you place instances of an instance of an instance, you must create an instance. self = [super init] means initializing the superclass method

A common mistake is writing

self = [[super alloc] init]; which returns an instance of the superclass, which is NOT what you want in the constructor of the / init subclass. You return an object that does not conform to the methods of the subclass, which may be misleading, and generates confusing errors in that they do not respond to the methods or identifiers found, etc.

self = [super init] necessary if the superclass has elements (variables or other objects) to initialize first before setting up members of the subclass. Otherwise, the objc runtime initializes them all to 0 or nil. (unlike ANSI C, which often allocates chunks of memory without clearing them at all)

And yes, initialization of the base class may fail due to errors due to memory, missing components, resource collection failures, etc., so checking for zero is reasonable and takes less than a few milliseconds.

+2
source

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


All Articles