iOS: -[NSTaggedPointerString objectForKey:]: crash - ios

This is the following code I have
id dataDict = [NSPropertyListSerialization
propertyListWithData:[dictionaryStr dataUsingEncoding:NSUTF8StringEncoding]
options:kNilOptions
format:NULL
error:NULL];
if ([dataDict isKindOfClass:[NSDictionary class]]) {
for (NSString *key in dataDict) {
// value string crashes with the following exception -[NSTaggedPointerString objectForKey:]:
NSString *value = [dataDict objectForKey:key];
}
}
There is a user who is having a crash occur on the following line NSString *value = [dataDict objectForKey:key]; according to crashlytics. This doesn't make any sense to me because I check beforehand if dataDict belongs to the NSDictionary class. Can someone please explain to me how I can prevent this crash?

Related

Unrecognized selector when parsing JSON

So I have the photograph below which is a screenshot I took of a JSON script I am trying to read from a URL. In the code section below, I managed to get the 'name' attribute and assign it to the 'name' NSString object, but when I try do the same for 'address' I get the following error:
[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x174260980
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndexedSubscript:]: unrecognized selector sent to instance 0x174260980'
whats the difference between how I did it inside 'venues' than inside 'location'? I tried many different ways to do it, some including arrays, but nothing seems to work.
NSURL *url = [NSURL URLWithString:urlString];
NSData *jsonData = [NSData dataWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
NSDictionary *responseDictionary = [dataDictionary objectForKey:#"response"];
NSDictionary *venueDictionary = [responseDictionary objectForKey:#"venues"];
for (NSDictionary *dict in venueDictionary) {
NSLog(#"%#", dict);
NSString *name = [dict objectForKey:#"name"];
NSDictionary *locationDictionary = [dict objectForKey:#"location"];
for (NSDictionary *locationDict in locationDictionary) {
NSString *address = [locationDict objectForKey:#"address"];
NSLog(#"%#", address);
}
}
You're expecting the "location" key to point to an array, but it doesn't. It points to an object, get rid of for (NSDictionary *locationDict in locationDictionary) { and just pull the address out of locationDictionary instead.

Failed to parse

I tried to parse this:
{"event":[{"event_id":"9","title":"event 10","welcome_logo":"20140715130727_252.png"}],"succeed":1}
This is my code:
NSString *strURL=[NSString stringWithFormat:#"http://loc**host/summit/event_login.php"];
NSData *dataURL=[NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult=[[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSArray *jsonArray=[NSJSONSerialization JSONObjectWithData:dataURL options:0 error:nil];
NSDictionary *element=[jsonArray objectAtIndex:0];
NSString *title = [element objectForKey:#"title"];
NSString *image = [element objectForKey:#"welcome_logo"];
But this guy came and disturbs me:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x9e7d010'
So I'm wondering what I did I do wrong. I tried to google it and many said that I assume to use an array when the data is actually a dictionary. Is that true? So what do I need to do?
Change
NSArray *jsonArray=[NSJSONSerialization JSONObjectWithData:dataURL options:0 error:nil];
NSDictionary *element=[jsonArray objectAtIndex:0];
to
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:dataURL options:0 error:nil];
NSArray *events = [jsonDict objectForKey:#"event"];
if ([events count] > 0) {
NSDictionary *element=[events objectAtIndex:0];
}
because your json data is in the form of dictionary. And event holds the list (array) of events. And do a count check before accessing objectAtIndex: from events.

Retrieve data from NSMutableArray

I am only starting with IOS Development and creating an IOS application (Final project) for my computer science class which uses JSON to get data from remote MySql database.
Here is the method I call from ViewDidLoad:
- (void)loadJSON
{
//-- Make URL request with server
NSHTTPURLResponse *response = nil;
NSString *jsonUrlString = [NSString stringWithFormat:#"http://vito-service.ru/studentsconnect/profile.php"];
NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
//-- Get request and response though URL
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];
//-- JSON Parsing
NSMutableArray *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSLog(#"Result = %#",result);
}
and NSLog prints the correct data:
Result = {
users = (
{
IdUser = 1;
facebook = "";
fullname = "";
instagram = "";
phonenumber = "";
username = moderas;
},
{
IdUser = 2;
facebook = "fb.com/petroffmichael";
fullname = "Michael Perov";
instagram = "#petroffmichael";
phonenumber = "(650)557-6168";
username = petroffmichael;
},
{
IdUser = 3;
facebook = "fb.com/testuser";
fullname = "Test User";
instagram = "#testuser";
phonenumber = "111 111-1111";
username = testuser;
}
);
}
But how can I retrieve this data as Strings to put them to the labels then? I tried lots of different options of creating Data objects, which I found here on stack overflow but I always get an "unrecognized selector sent to instance" error. I also tried creating a separate NSObject
#synthesize IdUser, username, fullname, phonenumber, facebook, instagram;
- (id) initWithUsername: (NSString *)uUsername andUserFullName: (NSString *)uFullname andUserPhonenumber: (NSString *)uPhonenumber andUserFacebook: (NSString *)uFacebook andUserInstagram: (NSString *)uInstagram andUserId: (NSString *)uId
{
self = [super init];
if (self) {
username = uUsername;
fullname = uFullname;
phonenumber = uPhonenumber;
facebook = uFacebook;
instagram = uInstagram;
IdUser = uId;
}
return self;
}
and then retrieving data with this method:
-(void)retrieveData
{
NSURL *url = [NSURL URLWithString:getDataUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
usersArray = [[NSMutableArray alloc] init];
for (int i=0; i<jsonArray.count; i++) {
NSString * uId = [[jsonArray objectAtIndex: i] objectForKey:#"IdUser"];
NSString * uUsername = [[jsonArray objectAtIndex: i] objectForKey:#"username"];
NSString * uFullname = [[jsonArray objectAtIndex: i] objectForKey:#"fullname"];
NSString * uPhonenumber = [[jsonArray objectAtIndex: i] objectForKey:#"phonenumber"];
NSString * uFacebook = [[jsonArray objectAtIndex: i] objectForKey:#"facebook"];
NSString * uInstagram = [[jsonArray objectAtIndex: i] objectForKey:#"instagram"];
[usersArray addObject:[[User alloc] initWithUsername:uUsername andUserFullName:uFullname andUserPhonenumber:uPhonenumber andUserFacebook:uFacebook andUserInstagram:uInstagram andUserId:uId]];
}
}
I have all the important #import's but I still get the same error:
2014-04-23 18:35:26.612 testimage[47155:70b] -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa72a8b0
2014-04-23 18:35:26.813 testimage[47155:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0xa72a8b0'
Can anybody please help to find the right solution?
Thanks!
After listening to multiple comments I changed the code but still receive an error..
I guess I am still doing something wrong
jsonArray is not an array anymore:
#property (nonatomic, strong) NSDictionary *jsonArray;
#property (nonatomic, strong) NSMutableArray *usersArray;
Then the method:
-(void)retrieveData
{
NSURL *url = [NSURL URLWithString:getDataUrl];
NSData *data = [NSData dataWithContentsOfURL:url];
jsonArray = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
usersArray = [[NSMutableArray alloc] init];
for (NSDictionary *dict in jsonArray)
{
NSString * uId = [dict objectForKey:#"IdUser"];
NSString * uUsername = [dict objectForKey:#"username"];
NSString * uFullname = [dict objectForKey:#"fullname"];
NSString * uPhonenumber = [dict objectForKey:#"phonenumber"];
NSString * uFacebook = [dict objectForKey:#"facebook"];
NSString * uInstagram = [dict objectForKey:#"instagram"];
[usersArray addObject:[[User alloc] initWithUsername:uUsername andUserFullName:uFullname andUserPhonenumber:uPhonenumber andUserFacebook:uFacebook andUserInstagram:uInstagram andUserId:uId]];
}
}
And I am still getting the same error:
2014-04-23 20:44:43.971 testimage[48285:70b] -[__NSCFString objectForKey:]: unrecognized selector sent to instance 0xa5547d0
2014-04-23 20:44:44.053 testimage[48285:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0xa5547d0'
Look closely at that output. It isn't what you say. What you have there is not an array — it's a dictionary with one key, #"users", whose value is an array. That's what it's telling you when it starts with { users = …. If you want the array, you'll need to retrieve it from the dictionary.
Try this:
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil];
NSMutableArray *result = [res objectForKey:#"users"];
This will do. The response isn't a NSArray. It is a Dictionary. First get the array from the dictionary and then use it as you are using. Hope this helps... :)
EDIT:
You can rewrite the loop by this:
for (NSDictionary *dict in jsonArray)
{
//dict is a NSDictionary. Just get value for keys
}

get value from NSDictionary not work

1.I get JSON data from web services and add It's to NSDictionary
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
2.Then I Show "dic" in NSLog It's present
{"query": [{"IndexNo": 1,"ID": "01","Picture": "img/food-48.png"}]}
3.Then I get data value from "dic" in to new NSDictionary "dt"
NSDictionary *dt = [dic objectForKey: #"query"];
4.Then I Show "dt" in NSLog It's present
({ID = 01;IndexNo = 1;Picture = "img/food-48.png";})
5.I want to get "ID" from "dt". I use this code
NSString *ID = [dt objectForKey: #"ID"];
but it's error
-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x8a40a60
Can you try this
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSArray *arr = [dic objectForKey: #"query"];
NSDictionary *dt = arr[0];
NSString *ID = [dt objectForKey: #"ID"];
[dic objectForKey: #"query"] returns an NSArray actually. Note these square brackets in the output from the first NSLog statement:
{"query": [...]}
Try this:
NSArray *dtArray = [dic objectForKey: #"query"];
NSDictionary *dt = dtArray[0];
NSString *ID = [dt objectForKey:#"ID"];
Or to use the key-value coding behavior of NSDictionary and NSArray:
NSString *ID = [[dic valueForKeyPath:#"query.ID"] firstObject];
NSDictionary *dt = [[dic objectForKey: #"query"] objectAtIndex:0];
NSString *ID = [dt objectForKey: #"ID"];
You are trying to get value for key from array. change these lines.
From your jsonStructure
{"query": [{"IndexNo": 1,"ID": "01","Picture": "img/food-48.png"}]}
object for key "query" is an array as {} is a dictionary and [] is an array. so do the following
NSArray *tempArray = [dt objectForKey:#"query"];
NSDictionary *tempDictionary = [tempArray objectAtIndex:0];
NSString *ID = [tempDictionary objectForKey:#"ID"];
Hope this helps.

How choose array in JSON

I have some problem in my project. So i used JSON for take some information.
When i have JSON with folders(example: i have NSDictionary with name Playlist in this playlist i have NSString name and album) for this i make this code:
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
NSDictionary *playlist =[allDataDictionary objectForKey:#"playlist"];
for (NSDictionary *diction in playlist) {
NSDictionary *artist = [diction objectForKey:#"artist"];
NSDictionary *song = [diction objectForKey:#"song"];
NSString *name = [artist objectForKey:#"name"];
NSString *namesong = [song objectForKey:#"name"];
[array addObject:name];
[array2 addObject:namesong];
}
[[self tableTrack]reloadData];
}
It's work perfect! BUT! When i don't have any folders, only, JSON without NSDictionary only NSStrings, how make? Sorry for my stupid question but really i tried write:
NSDictionary *allDataDictionary = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
NSString *name = [allDataDictionary objectForKey:#"name"];
NSString *namesong = [allDataDictionary objectForKey:#"name"];
[array addObject:name];
[array2 addObject:namesong];
}
[[self tableTrack]reloadData];
}
But i have error, also my app crashed:
* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x7615c90'
So, what i doing wrong?
The error says you have an NSArray, not an NSDictionary. This corresponds to a JSON array:
[
{ "name" : "fred" },
{ "name" : "jane" }
]
Here you have an array of dictionaries. You may want:
NSArray *people = [NSJSONSerialization JSONObjectWithData:webdata options:0 error:nil];
for (NSDictionary *person in people) {
NSLog(#"name is %#", person[#"name"];
}

Resources