0

My question is how to pass Null values in to NSMutableArray/NSArray.

But by searching google and going through different sites, i think that i have achieved my purpose. But, while trying i got a new doubt.

See the below code to understand my question

I am declaring a mutable array and initializing it with some objects initially and also i am passing a null in to it at the time of declaration itself.

Now when i run this code below

NSMutableArray * mar = [[NSMutableArray alloc]initWithObjects:@"first",@"last", nil];

[ar addObject:[NSNull null]];

NSLog(@"%@",mar); 

The o/p is :

(
    first,
    last,
    "<null>"
)

But my actual doubt is when i initialize the NSMutableArray as below

NSString *str;

NSMutableArray * mar = [[NSMutableArray alloc]initWithObjects:@"first",str,@"last", nil];

[ar addObject:[NSNull null]];

NSLog(@"%@",mar); 

The o/p is :

(
    first,
    "<null>"
)

The 2nd element in the array is "null", i understand that as i have passed a variable without assigning any value to it. It is printing null. But why are the remaining elements not printed.

According to what i know, the array will stop adding elements whenever it overcomes nil while initializing. But here, in this case. The o/p shows that the 2nd element is also null.(But not nil.)

Then why does the remaining elements are not printed.

UPDATING QUESTION

NSString* str;
NSLog(@"%@",str); 
NSMutableArray * mar = [[NSMutableArray alloc]initWithObjects:@"first",str,@"last", nil];

NSLog(@"%@",mar);

and the o/p for the above code is

(null) // for str an uninitialised NSString variable.


(
    first // first element in the array
)

If the value present in str is (null), how come the array encountered nil and stopped adding elements to array and printing them.

Now, someone answer this?

11
  • Why don't you just use "" , an empty string. You can just assign value to it when you want, and if you want to do comparison, can use if ans == "" Commented Mar 3, 2016 at 10:09
  • @Lee Yes, of course. I can use it just like you said to make the program work. But my question is different. My question is, why is it not working properly when i pass a variable without assigning any value. Commented Mar 3, 2016 at 10:15
  • @Mahesh The second element is null because you are adding [NSNull null] to it in the line before it is logged Commented Mar 3, 2016 at 10:17
  • 3
    In your second example, str is nil when you allocate the array, so it marks the termination of the initial object list. In effect, you only added one element when you initialized. Commented Mar 3, 2016 at 10:20
  • @Avi Yes, you are correct. It explains all. But you should have answered it. Instead of commenting. Commented Mar 3, 2016 at 10:37

2 Answers 2

3

initWithObjects uses nil as the marker for the last item. If you pass a nil (the unitialized str variable) to it as the 2nd item out of 4, only the first one is actually added.

[[NSMutableArray alloc]initWithObjects:@"first",str ?: [NSNull null],@"last", nil];

will fix your problem by replacing the nil with a [NSNull null].

Also see NSArray creation with variable argument lists

Sign up to request clarification or add additional context in comments.

2 Comments

@Mahesh I honestly cannot see any difference from your original question. The str is still nil and nil is still the marker for the end of parameter list, thus all the other items are not added. (null) is just a string representation of nil (see NSLog(@"%@", nil)).
Actually, the question has not been updated, as i thought it will. But anyway, this part of your answer cleared my doubt. (null) is just a string representation of nil (see NSLog(@"%@", nil))
-1

in objective-C, when iterating through Array if nil/null encountered then it gets treated as end of array.

this is the reason it is not printing remaining values.

in case you want clear diff between nil/null/Nil then check Difference between nil, NIL and, null in Objective-C

2 Comments

Completely and utterly wrong. An Objective-C array never contains nil, and [NSNull null] is not treated as the end of the array.
@gnasher729 when you initialize array in xcode dont you get last object as nil. that marks end of array. and in question, please refer code he is adding uninitialized object of NSSTring and not [NSNull null] and thats why array ends there and doesn't print next elements. if you agree pls dont down vote.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.