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.
The problem here is that when you get to that line,
idNumArrisn’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.[currentList valueForKey:@"idNumber"]is evidently returning a string (and there’s no reason to allocate the array immediately before that assignment. You probably want: