Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSMutableArray: addObject "nil"?

I am having troubling adding "nil" at the end of an array, I get "NSInvalidArgumentException" ?

NSMutableArray *k = [NSMutableArray arrayWithCapacity:10];

for (int i=0; i<9; i++){
    [k addObject: @"blank"];
}
[k addObject: nil]; //<-- NSInvalidArgumentException

I need to do all this item by item in a loop and then add the "nil".

Thanks

(I am then taking this array and initializing a 2D array. The problem is that I can't successfully "replaceObjectAtIndex" with an array without "nil". If I build the "k" with "initWithObjects: @"blank", @"blank", @"blank", ... nil" this will work. However writing 1000 blanks is a little much. So that is the purpose of the loop.)

////// HERE IS THE TRIAL AND ERROR CODE for init and building 2D Matrix for the purpose of reading and storing a matrix from a "CSV file" //////

///*
NSMutableArray *ppp = [NSMutableArray arrayWithCapacity:2];
NSMutableArray *kkk = [NSMutableArray arrayWithCapacity:20];

NSNull *myNull = [NSNull null];
for (int i=0; i<9; i++) {
    //[kkk addObject: [NSMutableString stringWithFormat: @"%d",i]];
    [kkk addObject: myNull];
}
//[kkk addObject: nil];

[ppp addObject:kkk];
[ppp addObject:kkk];
//*/


/*
// this is successful --> just uncomment this block and comment out the block above
[ppp addObject:[[NSMutableArray alloc] initWithObjects:
             @"z1",
             @"z2",
             @"z3",
             @"z4",
             @"z5",
             @"z6",
             @"z0gg",
             @"z0hh",
             @"z0ii",
             @"z0jj",
             nil
             ]];

[ppp addObject:[[NSMutableArray alloc] initWithObjects:
        @"a1",
        @"b2",
        @"c3",
        @"d4",
        @"e5",
        @"f6",
        nil
        ]];

*/

[[ppp objectAtIndex:0] replaceObjectAtIndex:1 withObject:@"HOTDOG"];
[[ppp objectAtIndex:1] replaceObjectAtIndex:1 withObject:@"HOHO"];
// HOHO will replace HOTDOG as well for the code not using "nil"
like image 424
jdl Avatar asked Jul 16 '11 00:07

jdl


People also ask

What is NSMutableArray?

The NSMutableArray class declares the programmatic interface to objects that manage a modifiable array of objects. This class adds insertion and deletion operations to the basic array-handling behavior inherited from NSArray . NSMutableArray is “toll-free bridged” with its Core Foundation counterpart, CFMutableArray .

How do I create an NSArray in Objective C?

Creating NSArray Objects Using Array Literals In addition to the provided initializers, such as initWithObjects: , you can create an NSArray object using an array literal. In Objective-C, the compiler generates code that makes an underlying call to the init(objects:count:) method.

What is swift NSArray?

NSArray is an immutable Objective C class, therefore it is a reference type in Swift and it is bridged to Array<AnyObject> . NSMutableArray is the mutable subclass of NSArray .


2 Answers

You cannot add nil to an NSMutableArray, and you will raise an exception if you try to.

There's NSNull, though:

NSNull *myNull = [NSNull null];
[myMutableArray addObject:myNull];

You might ask yourself why you're trying to do this, however.

like image 68
Alex Reynolds Avatar answered Nov 10 '22 01:11

Alex Reynolds


You don't need a nil as the last element in an array.

Don't confuse yourself with variadic methods like +arrayWithObjects: which receive a flexible number of arguments, and then need to find which one was the last. That's because in those methods implementation (as well as in variadic C functions) you can't retrieve the number of arguments passed, so nil marks the end.


As a general rule, you can nest loops in the same amount as of your dimensions. In your case, this would populate a "2D" array with different objects:

NSMutableArray *array = [NSMutableArray array];
int i,j;

for (i = 0; i < 2; ++i) {
    NSMutableArray *s_array = [NSMutableArray array];
    [array addObject:s_array];
    for (j = 0; j < 8; ++j) {
        [s_array addObject:[NSString stringWithFormat:@"%d%d", i, j]];
    }
}
like image 29
sidyll Avatar answered Nov 10 '22 03:11

sidyll