I can’t figure out why xcode continues to throw this error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-
[__NSCFString allKeys]: unrecognized selector sent to instance 0x9367330'
whenever I use this code
NSDictionary *nextArray = [_classDictionary
valueForKeyPath:@"response.page_records.page_record"];
for (NSDictionary *roster2 in nextArray) {
NSArray *keys = [roster2 allKeys];
if ([keys containsObject:@"title"]){
[pageName addObject:[roster2 objectForKey:@"title"]];
}
if ([keys containsObject:@"id"]) {
[pageID addObject:[roster2 objectForKey:@"id"]];
}
}
After trying to debug it I found out that the exception is only thrown when there is only one object for the key. For example when “nextArray” is this:
(
{
"class_id" = 0001;
"id" = 1234;
"title" = "First Page";
},
{
"class_id" = 0002;
"id" = 12345;
"title" = Assignments;
}
)
the code runs without an exception, but when the code is simply
{
"class_id" = 0001;
"id" = 1234;
"title" = "First Page";
}
The exception is thrown. Why would this be happening? The keys still contains the object title, even if there is only one object. Any other ideas why this exception is being thrown? Does it have something to do with the parenthesis? (total guess) Is there a better way to debug what is really going on?
EDIT: Here is the _userDictionary. What is the best way to have an ordered array of the objectforKey: @”title” and avoid the above error?
response = {
"status" = ok;
"page_records" = {
"page" = 1;
"page_count" = 1;
"records_per_page" = 50;
"total_record_count" = 2;
"page_record" = (
{
"class_id" = 0001;
"id" = 1234;
"title" = "Test page";
},
{
"class_id" = 0002;
"id" = 12345;
"title" = "testing page 2";
}
);
};
};
}
In your first example, you are iterating through an array of dictionaries, and hence,
NSArray *keys = [roster2 allKeys];is being called upon an array. However, in your second example, you have just one dictionary. It is not an array of dictionaries. So your loopfor (NSDictionary *roster2 in nextArray)is iterating through the keys of your dictionary, which are just strings, and not arrays. Hence, the error.First of all, your naming is confusing. An
NSDictionarynamednextArray? Try this: