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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With