I am using a singleton class to fetch JSON from a remote server (via NSURLConnection) - everything seeme to be fine except when I try to parse the JSON using JSONKit.
Here is some code
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[apiData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *realResponse = (NSHTTPURLResponse *)response;
if (realResponse.statusCode == 200)
{
apiData = [[NSMutableData alloc] init];
} else {
NSLog(@"Bad response = %i",realResponse.statusCode);
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *jsonData = [[NSString alloc] initWithData:apiData encoding:NSUTF8StringEncoding];
NSDictionary *deserializedData = [jsonData objectFromJSONString];
[self.delegate dataLoaded:deserializedData];
}
The error I get is at this line
NSDictionary *deserializedData = [jsonData objectFromJSONString];
-[__NSCFString objectFromJSONString]: unrecognized selector sent to instance 0x7fc1cd0
Any ideas what is going on here? This seems to be the normal way to parse JSON using JsonKit.
I have already made sure that JSON is valid...Does the string get corrupted somehow during appending in didReceiveResponse?
Figured it out... I had JSONKIt.h included in the project but for some weird reason, JSONKit.m was not included in the 'Compile Sources' under 'Build Phases' - once I added it manually it started working fine.