SBJSON将NSString解析为NSDictionary [英] SBJSON parsing NSString to NSDictionary
问题描述
我正在尝试使用SBJson 3.0.4将包含JSON数据的NSString解析为NSDictionary,但是当我这样做时,出现此错误:
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丢弃了webView:shouldInsertText:replacingDOMRange:givenAction中的未捕获异常:委托:-[__ NSCFString JSONValue]:无法识别的选择器已发送到实例0x6ab7a40"
"WebKit discarded an uncaught exception in the webView:shouldInsertText:replacingDOMRange:givenAction: delegate: -[__NSCFString JSONValue]: unrecognized selector sent to instance 0x6ab7a40"
据我所知(不是很远),我得到的JSON是有效的,所以我不知道为什么会这样.我的代码也可以正常编译… 在这里:
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
__NSCFString
类是NSString
接口的私有实现类,因此您可以假装它说NSString
.
The __NSCFString
class is the private implementation class for the NSString
interface, so you can just pretend it says NSString
.
所以我们看到您正在将JSONValue
消息发送到NSString
,并且NSString
说它无法识别该选择器. SBJson库将JSONValue
方法添加到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.
因此,我推断您尚未将NSObject+SBJson.o
链接到您的应用程序.如果您将SBJson源文件复制到您的应用程序中,请确保已在NSObject+SBJson.m
中进行了复制,并确保将其包含在目标的编译源"构建阶段中.
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.
如果您构建了SBJson库并将其链接到您的应用,则可能需要在链接器选项中甚至添加-ObjC
标志,甚至添加-all_load
标志.
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.
这篇关于SBJSON将NSString解析为NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!