Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the NSObject init method do anything?

Does it make sense to write self = [super init]; in a custom initialisation method while subclassing NSObject? I know it's necessary when subclassing any other class, because it might have a custom initialisation, but does the NSObject init method do anything?

like image 273
Tim Vermeulen Avatar asked Sep 14 '12 16:09

Tim Vermeulen


People also ask

What is Init method in Objective-C?

Out of the box in Objective-C you can initialize an instance of a class by calling alloc and init on it. // Creating an instance of Party Party *party = [[Party alloc] init]; Alloc allocates memory for the instance, and init gives it's instance variables it's initial values.

What does NSObject mean?

The root class of most Objective-C class hierarchies, from which subclasses inherit a basic interface to the runtime system and the ability to behave as Objective-C objects.

What is init () in Java?

In Java, an initializer is a block of code that has no associated name or data type and is placed outside of any method, constructor, or another block of code. Java offers two types of initializers, static and instance initializers.

What is Init method in Swift?

Swift init() Initialization is the process of preparing an instance of a class, structure, or enumeration for use. This process involves setting an initial value for each stored property on that instance and performing any other setup or initialization that is required before the new instance is ready for use.


1 Answers

An object isn’t ready to be used until it has been initialized. The init method defined in the NSObject class does no initialization; it simply returns self.

So basically you don't necessarily have to call [super init] in an NSObject subclass, but I still would recommend it. It's simply a better design. If you change the superclass it will still work.

Source: NSObject Class Reference.

like image 77
DrummerB Avatar answered Sep 27 '22 18:09

DrummerB