how to add nil to nsmutablearray? how to add nil to nsmutablearray? objective-c objective-c

how to add nil to nsmutablearray?


If you must add a nil object to a collection, use the NSNull class:

The NSNull class defines a singleton object used to represent null values in collection objects (which don’t allow nil values).

Assuming "array" is of type NSMutableArray:

....[array addObject:[NSNumber numberWithInt:2];[array addObject:@"string"];[array addObject:[NSNull null]];


You don't need to call [addObject:nil]

The nil in initWithObjects: is only there to tell the method where the list ends, because of how C varargs work. When you add objects one-by-one with addObject: you don't need to add a nil.


You can't add nil when you're calling addObject.