2 ways to Objective C custom init?

141 Views Asked by At

I am learning how to create custom INIT in Objective-C. There seems to be 2 ways to do so. Is there any difference between these two ways to create a custom init?

//First way of creating a custom init uses instancetype 

-(instancetype) init
{
   self = [super init];
   if (self) {
    //initialization code here
   }
   return self:

}

//Second way of creating a custom init uses ID

- (id)init {
    self = [super init];
    if (self) {
        //initialization code here
    }
    return self;
}
1

There are 1 best solutions below

0
On

it is safe to use instancetype all the time, but keep in mind that the implementation (.m) is not (usually) visible to other classes. So in many cases declaring instancetype in the .h and id in the .m would result in the same amount of compile-time safety.