I’m developing an iPhone application with latest SDK.
I have this code:
-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
NSError* error = nil;
NSObject* response =
[NSJSONSerialization JSONObjectWithData:webData
options:NSJSONReadingMutableContainers
error:&error];
if ([response isKindOfClass:[NSArray class]])
{
NSDictionary* resp = [response objectAtIndex:0];
This line, NSDictionary* resp = [response objectAtIndex:0]; doesn’t work. I get this compilation time error: No visible @interface for 'NSObject' declares the selector 'objectAtIndex:'.
I could do something like this:
NSArray* array = [NSArray initWithArray:response]; but I think it will create two objects and it will waste resources.
How do I cast a NSObject to NSArray?
This tutorial explains why I must use
id response =instead ofNSObject* response =.If I use
idI can send any message toresponseobject.So, If I have checked if
[response isKindOfClass:[NSArray class]]there won’t be any problem if I do[response objectAtIndex:0].And also, it won’t be any compilation time error.
I’ve added this answer because there will be more people with the same problem.