What's the result of passing just nil to arrayWithArray:?

cocoa, nsarray, nsmutablearray, objective-c

Solution

An empty array is returned.

An example implementation of `+arrayWithArray:` would be the following:

+(id) arrayWithArray:(NSArray *) arr
{
    NSMutableArray *returnValue = [NSMutableArray new];
    returnValue->objectsCount = [arr count];
    returnValue->objectsPtr = malloc(sizeof(id) * returnValue->objectsCount);
    [arr getObjects:returnValue->objectsPtr range:NSMakeRange(0, returnValue->objectsCount)];
    return returnValue;
}

Thus, if `arr` is null, `-count` returns 0, nothing is `malloc`'d, and nothing is copied, because a message sent to a nil object returns the default return value for that type, and does nothing else.

Problem

What happens when you pass `nil` to `arrayWithArray:`? Let's say I have the following code: `NSMutableArray *myArray = [NSMutableArray arrayWithArray:someOtherArray];` If `someOtherArray` happens to be `nil`, will `myArray` be `nil` or will it be an empty mutable array?

Original source