Daniel McCarthy
Daniel McCarthy

Reputation: 1416

iOS app crashing with -[NSCFString count]: when trying to get count of NSMutableArray if count is only 1

I'll keep this short and sweet, because I'm sure it is something simple that I'm missing. I'm trying to get the count for an NSMutableArray that can contain a variable number of objects (id numbers). The array is being created from JSon data & the array itself is created perfectly, regardless of whether there is a single object, or a thousand.

With regards to getting the count, If the array contains more than 1 object, everything works fine & dandy & count is returned successfully. However, if the array only has a single object/id number... the app crashes with -[NSCFString count]: error.

Here is the code I am using:

[request setCompletionBlock:^(void)
 {
     NSString *responseCurrent = [request responseString];
     NSMutableArray *json = [responseCurrent JSONValue];
     NSMutableArray *currentList = [[json valueForKeyPath:@"groupTO"] valueForKeyPath:@"currentList"];

     NSMutableArray *idNumArr = [[NSMutableArray alloc]init];
     idNumArr = [currentList valueForKey:@"idNumber"];
     debug(@"THIS IS ID ARRAY:  %@",idNumArr);

     debug(@"THIS IS COUNT OF THE ID ARRAY:  %@",[idNumArr count]);

     //  Ive also tried this to get the count, with no variation in results

     //  int countOfidNums = [idNumArr count];
     //  debug(@"count of arr:  %d",countOfidNums); 

I need to get this count to implement conditions to deal with other issues that arise from occurrences when this array only contains a single object.

I'm assuming the issue is stemming from the fact that objects in the array are strings, and when there is a single object, it is just coming as a string with no separating commas??? maybe??? help!!!!

ALSO: not sure if it makes a difference, but I am using ARC.

Upvotes: 0

Views: 1723

Answers (3)

Kevin
Kevin

Reputation: 56149

The problem here is that when you get to that line, idNumArr isn't an array, it's a string. If you don't believe me, print out [idNumArr class] just before that, or step in with the debugger.

 NSMutableArray *idNumArr = [[NSMutableArray alloc]init];
 idNumArr = [currentList valueForKey:@"idNumber"];

[currentList valueForKey:@"idNumber"] is evidently returning a string (and there's no reason to allocate the array immediately before that assignment. You probably want:

 NSMutableArray *idNumArr = [[NSMutableArray alloc]init];
 [idNumArr addObject:[currentList valueForKey:@"idNumber"]];

Upvotes: 2

rdelmar
rdelmar

Reputation: 104092

The problem seems to be that idNumArr is actually a string not an array. Add a log like this, and see what it gives you:

idNumArr = [currentList valueForKey:@"idNumber"];
NSLog(@"%@", idNumArr class]);

The fact that an array only has one string in it shouldn't cause this problem.

Upvotes: 0

khose
khose

Reputation: 763

idNumArr = [currentList valueForKey:@"idNumber"];

In this line, currentList is returning a NSString, and "count" is not a method in NSString (as you can check here: NSString class reference

Remember that here you are using pointers, and "value for key" returns an id pointer (no type associated). So you have to check that the object returned by "valueForKey" is actually an NSArray or not.

if([idNumArr isKindOfClass:[NSMutableArray class]])
//Then you can use "count"

PLEASE: Take into account that the code above hasn´t been tested or even syntax-checked. But search for that method "isKindOfClass" to check which object type you are using inside idNumArr.

Upvotes: 0

Related Questions