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:
-[__NSCFString JSONValue]: unrecognized selector sent to instance 0x6ab7a40
The __NSCFString
class is the private implementation class for the NSString
interface, so you can just pretend it says NSString
.
So we see that you are sending the JSONValue
message to an NSString
, and the NSString
says it doesn't recognize that selector. The SBJson library adds a JSONValue
method to the NSString
class using a category.
So I deduce that you haven't linked NSObject+SBJson.o
into your app. If you copied the SBJson source files into your app, make sure you copied in NSObject+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 -ObjC
flag to your linker options, or even the -all_load
flag.
来源:https://stackoverflow.com/questions/9420126/sbjson-parsing-nsstring-to-nsdictionary