iOS - Terminating app due to uncaught exception 'NSInvalidArgumentException' - ios

So I am really new at developing for iOS, and I've been doing my best, to search for an answer, to debug it and anything I could come up with.
Though, I haven't been able to find a solution to the issue.
I've been trying to fetch an external JSON document, which works fine, but when it comes to parsing it, it buggers up.
First of all, this is the error message I'm getting, the whole lot of it.
2013-01-31 22:40:19.261 demodh[6205:c07] View Loaded
2013-01-31 22:40:19.479 demodh[6205:c07] -[__NSCFStringcountByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7554f90
2013-01-31 22:40:19.480 demodh[6205:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x7554f90'
*** First throw call stack:
(0x1c93012 0x10d0e7e 0x1d1e4bd 0x1c82bbc 0x1c8294e 0x28d3 0xbd6589 0xbd4652 0xbd589a 0xbd460d 0xbd4785 0xb21a68 0x4615911 0x4614bb3 0x4652cda 0x1c358fd 0x465335c 0x46532d5 0x453d250 0x1c16f3f 0x1c1696f 0x1c39734 0x1c38f44 0x1c38e1b 0x1bed7e3 0x1bed668 0x14ffc 0x1d6d 0x1c95 0x1)
libc++abi.dylib: terminate called throwing an exception
(lldb)
And this is the code I'm using at the moment:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webData options:0 error:nil];
for(NSDictionary *dict in allDataDictionary)
{
if (![allDataDictionary isKindOfClass:[NSDictionary class]])
{
NSLog(#"2Unable to process temp array because it's an instance of %#", [allDataDictionary class]);
}
else
{
for(NSDictionary *deal in dict)
{
if (![deal isKindOfClass:[NSDictionary class]])
{
NSLog(#"Unable to process temp array because it's an instance of %#", [deal class]);
}
else
{
NSString *title = [deal objectForKey:#"title"];
NSLog(title);
}
}
}
}
}
And the JSON I'm loading is: Link
I hope you're able to assist me in finding a solution.

The problem is you're fast-enumerating a NSDictionary and expecting to get its values, when in fact you'll get its keys. When you try to fast-enumerate an NSString you get the assertion you're seeing. You probably wanted this:
for(NSObject *key in allDataDictionary) {
NSDictionary *dict = allDataDictionary[key];
...
for (NSObject *dealKey in dict) {
NSDictionary *deal = dict[dealKey];
}
...
Alternatively, if you really want to enumerate the values and don't need the keys:
for(NSDictionary *dict in [allDataDictionary allValues]) {
...

Related

Error : unrecognized selector sent to instance

I have been through similar questions, but not able to solve my issue.
The issue is i have a call to API and i am storing the response in a NSDictionary, the response is a single value like RED or GREEN:
saveBlock:^( NSDictionary *data, NSError **err )
{
if (! *err)
{
if (data)
{
NSLog(#"KEY %# - VALUE %#",data.allKeys,data.allValues); //Error because i am performing .allKeys on type NSString
NSString *str = data; // error because i am assigning NSDictionary to string.
}
}
else
{
NSLog(#" error is : - %#",*err);
}
}];
Aren't these two error messages contradicting each other ? Anyone explanation about this issue. Thank you.
EDIT The first NSLog gives this error : *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString allKeys]: unrecognized selector sent to instance 0x170664b80'
(This error appears only at runtime and not when i type data.allKeys)
And the second NSLog gives this: Incompatible pointer type, initialising NSString* with an expression of type NSDictionary *
(This is shown after typing, i don't know what this type of error is called :D)
It is a standard practice to check your response type and error message before performing any operation, sort of defensive coding.
eg.
DLog(#"Object:%#,Error:%#",responseObject,error);
if (error != nil) {
DLog(#"Got error from API:%#",error);
return ;
}
if (!responseObject) {
DLog(#"Response object is not valid, returning...");
return;
}
if (![responseObject isKindOfClass:[NSDictionary class]]) {
DLog(#"Incorrect type of returned object, expecting NSDictionary, got:%#",[responseObject class]);
return;
}
Also I am pretty sure you are getting a NSString cause you don't get an error when assigning a NSDictionary to a NSString
Webservice response data 'data' should be NSDictionary, but in your case you are getting NSString. For NSString class, there is no allKeys method available, that is why you are getting crash. One thing is to validate for NSDictionary like.
if (data && [data isKindOfClass:[NSDictionary class]]) {
NSLog(#"KEY %# - VALUE %#",data.allKeys,data.allValues); //Error because i am performing .allKeys on type NSString
NSString *str = data; // error because i am assigning NSDictionary to string
}
Or remove NSLog

getting error when appending array NSDictionary [duplicate]

This question already has answers here:
How to parse received response [closed]
(6 answers)
Closed 9 years ago.
I was trying to get the JSON data to an array. Below is the code... I dont know where am I going wrong..
When I keep a Log for arrayJSON Below is the result :
Array Result: [{"ID_PROJECT":1,"NM_PROJECT":"TED"},{"ID_PROJECT":2,"NM_PROJECT":"DL"},{"ID_PROJECT":3,"NM_PROJECT":"PERD"},{"ID_PROJECT":4,"NM_PROJECT":"EPRS "},{"ID_PROJECT":5,"NM_PROJECT":"MSCS"}]
NSArray *arrayJSON = [NSJSONSerialization JSONObjectWithData:urlData
options:NSJSONReadingAllowFragments
error:&error];
NSMutableArray *arrayResult = [NSMutableArray array];
for(NSDictionary *dictJSON in arrayJSON){
[arrayResult addObject: [dictJSON objectForKey:#"NM_PROJECT"]];
}
NSLog(#"Array Result: %#", arrayResult);
Here is the error code :
014-02-25 15:07:40.602 Demologin[1146:c07] * Terminating app due to
uncaught exception 'NSInvalidArgumentException', reason:
'-[__NSCFString countByEnumeratingWithState:objects:count:]:
unrecognized selector sent to instance 0x715f430'
* First throw call stack: (0x1ca3012 0x114fe7e 0x1d2e4bd 0x1c92bbc 0x1c9294e 0xad28 0x1061c7 0x106232 0x553d5 0x5576f 0x55905 0x5e917
0x2296c 0x2394b 0x34cb5 0x35beb 0x27698 0x1bfedf9 0x1bfead0 0x1c18bf5
0x1c18962 0x1c49bb6 0x1c48f44 0x1c48e1b 0x2317a 0x24ffc 0x26ad 0x25d5)
libc++abi.dylib: terminate called throwing an exception
Array Result: [{"ID_PROJECT":1,"NM_PROJECT":"TED"},{"ID_PROJECT":2,"NM_PROJECT":"DL"},{"ID_PROJECT":3,"NM_PROJECT":"PERD"},{"ID_PROJECT":4,"NM_PROJECT":"EPRS "},{"ID_PROJECT":5,"NM_PROJECT":"MSCS"}]
NSMutableArray *array = [Array Result JSONValue];
[array retain];
NSLog(#"Array Result: %#", array);

__NSCFConstantString objectForKey unrecognized selector sent to instance error

I basically get this error
'NSInvalidArgumentException', reason: '-[__NSCFConstantString objectForKey:]: unrecognized selector sent to instance 0x581f0'
on my program. I think it refers to this call I make,
if (data != nil) {
if([data objectForKey:#"username"]){
// NSArray *check= [[NSArray alloc]init];
//check=[data allValues];
[dict setObject:[data allValues] forKey:#"args"];
}else{
[dict setObject:[NSArray arrayWithObject:data] forKey:#"args"];
}
at the setObject:[data allValues]. I don't know why it gives that error but data is an NSDictionary and I'm getting all the values and placing it in an array.
Is the error happening here:
if([data objectForKey:#"username"]){
I assume so, as that is the only place objectForKey seems to be called. You are calling it on a variable called 'data', which i'm guessing simply is not a dictionary. You should NSLog its type to see.

Why do i get an error comparing NSString's? (-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance)

I have a NSMutableArray (_theListOfAllQuestions) that I am populating with numbers from a file. Then I compared the objects in that array with qNr (NSString) and I got error. I even casted the array to another NSString, _checkQuestions, just to be sure I am comparing NSStrings. I tested using item to compare also.
-(void)read_A_Question:(NSString *)qNr {
NSLog(#"read_A_Question: %#", qNr);
int counter = 0;
for (NSString *item in _theListOfAllQuestions) {
NSLog(#"item: %#", item);
_checkQuestions = _theListOfAllQuestions[counter]; //_checkQuestion = NSString
NSLog(#"_checkQuestions: %#", _checkQuestions);
if ([_checkQuestions isEqualToString:qNr]) {
NSLog(#">>HIT<<");
exit(0); //Just for the testing
}
counter++;
}
When running this code i get the following NSLog:
read_A_Question: 421
item: 1193
_checkQuestions: 1193
...and error:
-[__NSCFNumber isEqualToString:]: unrecognized selector sent to instance 0x9246d80
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber
isEqualToString:]: unrecognized selector sent to instance 0x9246d80'
I do believe that I still comparing NSString with a number of some sort but to me it looks like I am comparing NSString vs. NSString?
I could really need some help here to 1) understand the problem, 2)solve the problem?
Replace this line
if ([_checkQuestions isEqualToString:qNr])
with
if ([[NSString stringWithFormat:#"%#",_checkQuestions] isEqualToString:[NSString stringWithFormat:#"%#",qNr]])
Hope it helps you..
Your _theListOfAllQuestions array has NSNumber objects and not NSString objects. So you cant use isEqualToString directly.
Try this,
for (NSString *item in _theListOfAllQuestions) {
NSLog(#"item: %#", item);
_checkQuestions = _theListOfAllQuestions[counter]; //_checkQuestion = NSString
NSLog(#"_checkQuestions: %#", _checkQuestions);
if ([[_checkQuestions stringValue] isEqualToString:qNr]) {
NSLog(#">>HIT<<");
exit(0); //Just for the testing
}
counter++;
}

Weird crash while accessing array objects at a given index in IOS

We have the following method where we are trying to access an array object at a given index. The array is resultArr. When we do a resultArr count it gives us a result of 13. So we know that the array is not null but when we try to do objectAtIndex it crashes with the error.
Function:
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray *keys = [json allKeys];
NSLog(#"keys: %#",keys);
NSArray* htmlAttributions = [json objectForKey:#"html_attributions"]; //2
NSArray* resultArr = (NSArray *)[json objectForKey:#"result"]; //2
NSArray* statusArr = [json objectForKey:#"status"]; //2
NSLog(#"htmlAttributions: %#",htmlAttributions);
NSLog(#"result: %#", resultArr); //3
NSLog(#"status: %#", statusArr); //3
NSLog(#"resultCount: %d",[resultArr count]);
[resultArr objectAtIndex:0];
}
Error:
2012-04-01 22:31:52.757 jsonParsing[5020:f803] resultCount: 13 2012-04-01 22:31:52.759 jsonParsing[5020:f803] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6d2f900 2012-04-01 22:31:52.760 jsonParsing[5020:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6d2f900'
*** First throw call stack:
Thank you.
The error message is fairly descriptive. One of the objects that your code expects to be an NSArray is actually an NSDictionary. You cannot access fields inside of an NSDictionary by using NSArray methods (and casting from NSDictionary* to NSArray* will not convert an NSDictionary into an NSArray).
This would mean that inside of the JSON, one of your elements was serialized as an object/associative array instead of as a plain array. You can easily determine which one by looking at your JSON data as text, and finding the item that uses { and } instead of [ and ].
You are saying
NSArray* resultArr = (NSArray *)[json objectForKey:#"result"]; //2
But that does not make this object ([json objectForKey:#"result"]) an NSArray. It is an NSDictionary, and sending it a message that NSDictionary does not respond to (objectAtIndex:) causes a crash.
You were able to send it the count message without crashing because NSDictionary does happen to respond to the count message. But your preconception that this is an array is still mistaken.
You cannot cast an NSDictionary* to an NSArray* as you tried to do with this line: NSArray* resultArr = (NSArray *)[json objectForKey:#"result"];, then call -objectAtIndex.

Resources