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?

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Mahesh
  • 40
  • 10
  • 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 == ""` – Lee Mar 03 '16 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. – Mahesh Mar 03 '16 at 10:15
  • @Mahesh The second element is null because you are adding [NSNull null] to it in the line before it is logged – ZeMoon Mar 03 '16 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. – Avi Mar 03 '16 at 10:20
  • @Avi Yes, you are correct. It explains all. But you should have answered it. Instead of commenting. – Mahesh Mar 03 '16 at 10:37
  • @Lee As i am new here. I don't know how to post like that. Thanks, for the good looking edit. – Mahesh Mar 03 '16 at 10:40
  • Log the array _before_ you add the other information. Then read the documentation of initWithObjects _carefully_. Your code does exactly what I would expect it to do. – gnasher729 Mar 03 '16 at 10:50
  • And thank you for advising Avi about the proper way to use stackoverflow. In reality, it's a beginner's mistake and not really worth an answer. – gnasher729 Mar 03 '16 at 10:51
  • @gnasher729 Everyone is a beginner at certain point of time in life. So, don't mock others. – Mahesh Mar 03 '16 at 11:53
  • Your updated question has already been answered by Sulthan – ZeMoon Apr 13 '16 at 11:45
  • Yes, it has been answered. Thanks, everyone – Mahesh Apr 13 '16 at 14:05

2 Answers2

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

Community
  • 1
  • 1
Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • @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)`). – Sulthan Apr 13 '16 at 11:37
  • 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)) – Mahesh Apr 13 '16 at 14:04
-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

Community
  • 1
  • 1
vivek
  • 447
  • 4
  • 6
  • Completely and utterly wrong. An Objective-C array _never_ contains nil, and [NSNull null] is not treated as the end of the array. – gnasher729 Apr 13 '16 at 12:11
  • @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. – vivek Apr 13 '16 at 12:20