How to add null or nil value in "dynamic NSArray" in Objective-C

First you can't add elemets in NSArray, you must use NSMutableArray instead. Second thing you can not add nil in your NSMutableArray.

So, you should add empty string instead of nil something like,

NSMutableArray *arr = [[NSMutableArray alloc]init];

[arr addObject:@""];

or you can add NSNull Object like,

[arr addObject:[NSNull null]];

And you can check that string is empty or not when want to display in UI something like(if added empty string),

NSString *tempStr = [arr objectAtIndex:0];

if (tempStr.length > 0) {

    NSLog(@"string is not empty.");
}
else{

    NSLog(@"Sring is empty");
}

We cannot add nil directly inside a collection like NSMutableArray as it will raise an exception. If at all it is required to add nil inside collections like NSMutableArray, we can do so by using a singleton class NSNull.

For instance, we have a array of type NSMutableArray, we can add nil using NSNull as-

[array addObject:@"string"];
[array addObject:[NSNull null]];

... and so on...

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