For example in low memory conditions. I'm reading third party code and I saw this
NSMutableArray *array = [NSMutableArray array];
if (array != nil) {
[array addObject:anObject];
}
I've never check this after creating an array that way nor alloc+init but now I'm in doubt.
Technically, it could return nil.
The probability of such a failure at that point is incredibly unlikely. Your app would probably crash or abort before a nil were returned. Consider it about as unlikely as malloc returning NULL.
My implementations have used checked object creation for years - I cannot remember ever seeing [NSMutableArray array] return nil in that time in development/testing.
Note that this answer is specific to [NSMutableArray array], not any/every initializer/constructor in existence. [NSMutableArray array] is nearly the most 'consistent' class of initializer/constructor in this regard because there is no reason for it to fail under normal circumstances.
Yes. An object instantiation that fails for any reason should return nil unless the method states otherwise.
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