-1

I created a multidimensional array as follows:

NSMutableArray *subArray = [NSMutableArray arrayWithObjects:[NSString string], [NSNumber       numberWithInt:0], [NSMutableArray array], nil];
self.dataArray = [[NSMutableArray alloc] initWithCapacity:9];
for (int i = 0; i < 9; i++) {
    [self.dataArray addObject:subArray];
}

then when I try to access and change values like this

NSNumber *num = self.dataArray[0][1];
int numInt = [num intValue];
NSNumber *newNum = [NSNumber numberWithInt:numInt + 1];
[self.dataArray[0][1] addObject:newNum];
// add item to dataArray
NSMutableArray *tmpArr= self.dataArray[0][2];
[tmpArr addObject:item];
[self.dataArray[0][2] addObject:tmpArr];

but I'm getting

-[__NSCFNumber addObject:]: unrecognized selector sent to instance

what exactly is the problem, I don't understand, thanks in advance!

adambargh
  • 119
  • 1
  • 5
  • 11

2 Answers2

1

In the first line you are treating the object in the array as a NSNumber (which it obviously is):

NSNumber *num = self.dataArray[0][1];

And here you treat the exact same object like an NSMutableArray:

[self.dataArray[0][1] addObject:newNum];

That won't work, because that object is an instance of NSNumber.

I don't know what you achieve so I can't help you with the correct code, but that's where your problem is. Maybe you just wanted to write:

[self.dataArray[0][2] addObject:newNum];

You should probably stop to use an "inner array" as data storage and switch to using a proper subclass. Currently your code is pretty much unreadable, using proper Objects to store your values would improve it a lot.


Btw, your multidimensional array is actually just one dimensional, because you add the exact same array multiple times.

You probably want to do this:

for (int i = 0; i < 9; i++) {
    NSMutableArray *subArray = [NSMutableArray arrayWithObjects:[NSString string], [NSNumber numberWithInt:0], [NSMutableArray array], nil];    
    [self.dataArray addObject:subArray];
}
Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
0
NSNumber *num = self.dataArray[0][1];
int numInt = [num intValue];
NSNumber *newNum = [NSNumber numberWithInt:numInt + 1];
[self.dataArray[0][1] addObject:newNum];       // 3. also, possibly here
// add item to dataArray
NSMutableArray *tmpArr= self.dataArray[0][2];  // 1. here
[tmpArr addObject:item];                      // 2. and here
[self.dataArray[0][2] addObject:tmpArr];

You're getting this error because the first line I marked, tmpArr is actually of type NSNumber. NSNumber is a class cluster, which is why you're seeing __NSCFNumber throw the error. All that is, is just a private subclass of NSNumber.

So the error is being thrown because you're trying to call addObject on a type of object that doesn't support it. Personally I wouldn't store more than one type of object in an array, but I don't know exactly what you're doing. Assuming you don't change the way you're storing things, what you can do is this:

NSMutableArray *tmpArr= self.dataArray[0][2];  
if ([tmpArr isKindOfClass:[NSMutableArray class])
{
    [tmpArr addObject:item];                      
}
else 
{ 
    NSLog(@"Woops, trying to add an object to something that's not a mutable array");
}

You would have to do this everytime you try to store an object into an array that you're pulling out of self.dataArray. What this does is verify that tmpArr is what you think it is.

Alternatively, you could check if it responds to addObject

if ([tmpArr respondsToSelector:@selector(addObject:)])
{
    [tmpArr addObject:item];
}

The second way doesn't care what class it is, only if the method addObject can be used.

Chris
  • 7,270
  • 19
  • 66
  • 110