What is the result of passing only a null value to an arrayWithArray :?

What happens when you pass nilup arrayWithArray:?

Let's say I have the following code:

NSMutableArray *myArray = [NSMutableArray arrayWithArray:someOtherArray];

If it someOtherArrayturns out nil, will it myArraybe nilor will it be an empty mutable array?

+3
source share
1 answer

An empty array is returned.

An example implementation +arrayWithArray:will be as follows:

+(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 it arris null, it -countreturns 0, there is nothing malloc'd, and nothing is copied, since the message sent to the nil object returns the default value for this type and does nothing.

+10
source

All Articles