NSMutableArray: addObject "nil"?

Viewed 11772

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"
3 Answers
Related