0

I'm trying to parse the following JSON information that comes out of my PHP file:

{
"netSales":0,
"voidSales":0,
"discountSales":0,
"guestCount":null,
"servedCount":null,
"loggedIn":9
}

My string is set something like this:

       NSURL * url = [NSURL URLWithString:salesStr];
            NSData * data = [NSData dataWithContentsOfURL:url];



            json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];


            NSLog(@"response type is %@",[json class]);

            //Set up our cities array



            arrayOfStore = [[NSMutableArray alloc]init];

            for (int i = 0; i < json.count; i++)



            {


                NSString * netSales = json[i][@"netSales"];
                NSString * voids = json[i][@"voidSales"];
                NSString * discounts = json[i][@"discountSales"];
                NSString * guestCount = json[i][@"guestCount"];
                NSString * peopleServed = json[i][@"servedCount"];
                NSString * employeesClock = json[i][@"loggedIn"];




                Store * myStore = [[Store alloc]initWithNetSales: (NSString *) netSales andVoids: (NSString *) voids andDiscounts: (NSString *) discounts andGuestCount: (NSString *) guestCount andPeopleServed: (NSString *) peopleServed andEmployeesClock: (NSString *) employeesClock];




                [arrayOfStore addObject:myStore];

But it's returning the error message:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x8ddb930'

What does this error mean?

EDIT: I extended my code a bit.

1 Answer 1

2

Your error message, "[__NSCFDictionary objectAtIndex:]: unrecognized selector sent", is informing you that you attempted to call objectAtIndex (an array method) on an object that was really a dictionary.

Your code snippet appears to assume that the JSON is an array of dictionaries. But on the basis of what you've shared with us, it looks like a simple dictionary. That is consistent with the error message you received. You could remedy this by just calling objectForKey, and eliminate the call to objectAtIndex.

By the way, netSales appears to be a number, not a string, so use NSNumber rather than NSString.

So, putting those together, I think you'd want:

NSNumber *netSales = json[@"netSales"];  // or [json objectForKey:@"netSales"];
Sign up to request clarification or add additional context in comments.

4 Comments

I edited the code in my above post. Does your answer still remain if my code looks like this?
Yes. It looks like you're trying to iterate through an array, but there is no array.
json variable is of what type ??
@SquirrelInControl My answer is based upon the JSON you shared with us (which is a simple dictionary, not an array of dictionaries). If the sample JSON is representative, then this is the case. Look at the error, "[__NSCFDictionary objectAtIndex:]: unrecognized selector sent" ... that means that you tried to call objectAtIndex on an object that wasn't an array, but rather was a dictionary. This is also consistent with my diagnosis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.