Foundation classes, check for nil after alloc init

79 views Asked by At

I need a mutable string in a critical function of my application. I do this:

NSMutableString *string = [NSMutableString alloc] init];

Is there any reason whatsoever that my string may not have been allocated/ initialized. Should I check for:

if(nil == string)
{
   // Inform the caller we failed to get a mutable string
   // return false;
}
1

There are 1 answers

0
justin On

It's very unlikely to be nil in this scenario, but not guaranteed.

For example: You must consider that the implementation has the ability to return nil when/if an allocation fails (the object itself requires a heap allocation, and perhaps its innards do also).

Panic handlers (in C) often employ static arrays to step around this possibility.