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;
}
it is safe to use
instancetypeall the time, but keep in mind that the implementation (.m) is not (usually) visible to other classes. So in many cases declaringinstancetypein the .h andidin the .m would result in the same amount of compile-time safety.