iOS Cocoa NSArrayI length]: unrecognized selector sent to instance - ios

I am trying to determine what is causing this error:
2014-06-08 20:40:44.076 Database[8656:70b] -[__NSArrayI length]:
unrecognized selector sent to instance 0x8a44050
Here is the code.
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
This is the breaking point where the error occurs: NSLog([json allKeys]);
SAMPLE OF THE JSON BEING USED AS INPUT:
{"1":{"key":"1","contentONE":"aaa","contentTWO":"testing"},"2":{"key":"2","contentONE":"bbb","contentTWO":null},"3":{"key":"3","contentONE":"ccc","contentTWO":"testing"}}

[json allKeys] returns an NSArray, while NSLog expects a formatting string. Try this:
NSLog(#"%#", [json allKeys]);

Related

Cannot convert subArray form json string into NSArray

I have a json string that I have received form some API, which looks like this:
[{"id": 2, "title": "Hello world!", "source": "htp://abc.com/hello_world", "blog": "abc.com", "rating": 0, "date": "2014-08-16T15:44:29Z", "tags": ["programming"]}, {"id": 1, "title": "Why we do not fly to space?", "source": "htp://habrahabr.ru/post/233119/", "blog": "habrahabr.ru", "rating": 5, "date": "2014-08-13T15:56:34Z", "tags": ["space", "science", "future"]}]
I receive it like this:
NSDictionary *JSONDictionary = [NSJSONSerialization JSONObjectWithData:returnData options:kNilOptions error:&error];
NSArray *jsonArray = (NSArray*)JSONDictionary;
for (int i=0; i<=jsonArray.count-1; i++) {
NSDictionary *item = [jsonArray objectAtIndex:i];
. . .
NSString* tags = [item objectForKey:#"tags"];
. . .
}
So now I have this NSArray. I can extract everything except tags.
I have tried:
NSLog(#"Tags: %#", tags);
NSArray *json = [NSJSONSerialization
JSONObjectWithData:[tags dataUsingEncoding:NSUTF8StringEncoding]
options:kNilOptions
error:&error];
This gives me exception:
-[__NSCFArray dataUsingEncoding:]: unrecognized selector sent to instance 0x109399810
2014-08-17 14:45:48.224 ReadLater[38625:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray dataUsingEncoding:]: unrecognized selector sent to instance
Then, I have tried this:
NSArray *strings = [tags componentsSeparatedByString:#","];
This gives me exception:
-[__NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x1096a6680
2014-08-17 14:50:57.676 ReadLater[38758:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray componentsSeparatedByString:]: unrecognized selector sent to instance 0x1096a6680'
When I print out tags variable, it looks like this:
Tags: (
space,
science,
future
)
This is everything that I could possibly tell you about this problem. Hope this gives you idea of whats wrong, coz I suspect that I simply don't know something about this particular case.
Thank you very much.
Tags is not a string, it is an array. So, to properly get the tags, use this
NSArray *tags = [item objectForKey:#"tags"];
To get the tag from the first dictionary in your JSON, you would do this
NSString *firstTag = tags[0]; //this would be "programming"

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.

Parsing JSON Array in iOS

I'm attempting to read a JSON array from my php generated file at www.tiritium.com/standings.php and it works just fine on Android but I cannot get it to read out of the file. It opens it properly but I can't get get the NSDictionary into a NSArray.
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:responseData //1
options:kNilOptions
error:&error];
NSArray* latestNames = [json objectForKey:#"name"]; //2
NSLog(#"name: %#", latestNames); //3
I've narrowed down the error to //2 but I can't seem to figure out what is going on. I'm not sure if it is with how my data is being output from the script or with my code in the app itself.
2013-08-14 16:39:51.992 Derby Days[7951:c07] -[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8a297b0
2013-08-14 16:39:51.993 Derby Days[7951:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8a297b0'
Is the error message I am receiving.
Read the exception
-[__NSCFArray objectForKey:]: unrecognized selector
The root of your JSON is an array, not a dictionary.
Your JSON file represents an array, not a dictionary. If you need to get an array of names out of it, you use key value coding like NSArray *names = [json valueForKey:#"name"];. This will give you an array of NSStrings.

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