how to add nil to nsmutablearray?

IphoneObjective CNsmutablearrayNull

Iphone Problem Overview


NSArray *array = [[NSArray alloc] initWithObjects:@"ΕΛΤΑ",
                      @"ΕΛΤΑ COURIER", @"ACS", @"ACS ΕΞΩΤΕΡΙΚΟ", 
                      @"DHL", @"INTERATTICA", @"SPEEDEX", 
                      @"UPS", @"ΓΕΝΙΚΗ ΤΑΧΥΔΡΟΜΙΚΗ", @"ΜΕΤΑΦΟΡΙΚΕΣ ΕΞΩΤΕΡΙΚΟΥ", nil];

This is working because it has nil at the end.

But I add objects like this: addObject:name etc... So at the end I have to add nil I do this addObhect:nil but when I run the app it still crashes at cellForRowAtIndexPath:

how can I do this work?

Ok, I dont have to add nil

What is the reason that my app crashes then?

Iphone Solutions


Solution 1 - Iphone

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]];

Solution 2 - Iphone

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.

Solution 3 - Iphone

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

Solution 4 - Iphone

If you really want a Null-ish item in your collection, NSNull is there for that.

Solution 5 - Iphone

You need to add NSNull class and the best way to do it is like this:

NSArray *array = @[ @"string", @42, [NSNull null] ];

I personally recommend to use a specific value like 0 instead of null or nil in your design of your code, but sometimes you need to add null.

There is a good explanation from this Apple reference.

Solution 6 - Iphone

nil is used to terminate the array

Solution 7 - Iphone

nil is not an object that you can add to an array: An array cannot contain nil. This is why addObject:nil crashes.

Solution 8 - Iphone

pass your object through this method when adding to array to avoid attempt to insert nil object from objects crashes.

-(id) returnNullIfNil:(id) obj  {
    return (obj == nil) ? ([NSNull null]) : (obj);
}

[NSNull null] returns an null object which represents nil.

Solution 9 - Iphone

You can't add an object to an NSArray because that class is immutable. You have to use NSMutableArray if you want to change the array after it is created.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionstefanosnView Question on Stackoverflow
Solution 1 - IphoneAdrian KosmaczewskiView Answer on Stackoverflow
Solution 2 - IphoneMike WellerView Answer on Stackoverflow
Solution 3 - Iphonemr-skView Answer on Stackoverflow
Solution 4 - IphoneDavid GrantView Answer on Stackoverflow
Solution 5 - IphonemertView Answer on Stackoverflow
Solution 6 - IphoneTomenView Answer on Stackoverflow
Solution 7 - IphonemouvicielView Answer on Stackoverflow
Solution 8 - IphoneManjuhereView Answer on Stackoverflow
Solution 9 - IphoneTechZenView Answer on Stackoverflow