[Solved] What is the meaning of init method in objective c?


So if it’s objective c, it’s init and not init() 🙂 just saying… kinda big things…
See in java, when you are doing a

new MyClass()

2 things happened:

  • the compiler book some space in memory to store your new instance
  • the MyClass constructor is called.

now, in objective-C, those 2 thing are separated into 2 methods:
– alloc
– init

so when you do want to make a new object, you have to call these 2 methode

MyClass * a = [MyClass alloc]; // this will return a pointer to a space in memory big enough to store an instance of MyClass
a = [a init]; // this will call the init method on a and return the pointer initialized with all iVar to 0 plus whatever you did by yourself in the init method.

usually, it’s done in one line

MyClass * a = [[MyClass alloc] init];

That’s why it is so convenient to have init a instance class that returns the actual instance

solved What is the meaning of init method in objective c?