Question

I'm new in XCode and Objective-C and try to learn it. I have tried to search on the net about this problem and can't find the solution that works for me. Maybe because I'm using a bad keyword to search. Can you help me for below problems? It seems that this is a very novice problem. I'm banging my head at the wall right now....

I have a JSON response:

{"message":"success","personal":{"userID":"111","companyID":"11","nameUser":"User Name Here","fotoURL":"http:\/\/myurl.com\/data\/photo\/11\/111.jpg"}}

I have succeed to contain userID, companyID and nameUser into variables in AppDelegate variables. (I'm going to use this variables in multiple view controllers).

Currently I'm using this:

NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:urlData options:NSJSONReadingMutableContainers error:&error];
AppDelegate *myAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
myAppDelegate.userID = [jsonData [@"personal"][@"userID"] intValue];
myAppDelegate.companyID = [jsonData [@"personal"][@"companyID"] intValue];
myAppDelegate.fotoURL = [jsonData [@"personal"][@"fotoURL"] stringValue];

The error message is: -[__NSCFString stringValue]: unrecognized selector sent to instance 0x8e89ae0

But I'm failing to contain fotoURL. Would you mind to show where I do wrong? And is this a best practice?

I have browsed to search how to parse JSON and find many methods, such as using [nsdictionary_object objectForKey:@"json-node"] etc. But none are succeeded.

Thank you before

Was it helpful?

Solution

fotoURL is already a string, so you don't need to call stringValue on it.

You can tell this by [__NSCFString stringValue], the __NSCFString means the object is of class __NSCFString (which is effectively an NSString).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top