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.