I’m trying to parse an NSString that contains JSON data into an NSDictionary using SBJson 3.0.4, but when I do it, I get this error:
“WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: -[__NSCFString JSONValue]: unrecognized selector sent to instance 0x6ab7a40”
As far as I know (which isn’t very far), the JSON I’m getting is valid, so I don’t know why this is happening. My code compiles fine too…
Here it is:
NSString *tempURL = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true",userInput.text];
NSURL *url = [NSURL URLWithString:tempURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReturnCacheDataElseLoad
timeoutInterval:30];
// fetch the JSON response
NSData *urlData;
NSURLResponse *response;
NSError *error;
// make the synchronous request
urlData = [NSURLConnection sendSynchronousRequest:urlRequest
returningResponse:&response
error:&error];
// construct a String around the Data from the response
NSString *data = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSDictionary *feed = [data JSONValue];
The important part of the error message is this:
The
__NSCFStringclass is the private implementation class for theNSStringinterface, so you can just pretend it saysNSString.So we see that you are sending the
JSONValuemessage to anNSString, and theNSStringsays it doesn’t recognize that selector. The SBJson library adds aJSONValuemethod to theNSStringclass using a category.So I deduce that you haven’t linked
NSObject+SBJson.ointo your app. If you copied the SBJson source files into your app, make sure you copied inNSObject+SBJson.m, and make sure it is included in the “Compile Sources” build phase of your target.If you built an SBJson library and are linked your app to that, you may need to add the
-ObjCflag to your linker options, or even the-all_loadflag.