2

我正在尝试使用 SBJson 3.0.4 将包含 JSON 数据的 NSString 解析为 NSDictionary,但是当我这样做时,出现此错误:

“WebKit 丢弃了 webView 中未捕获的异常:shouldInsertText:replacingDOMRange:givenAction: delegate: -[__NSCFString JSONValue]: unrecognized selector sent to instance 0x6ab7a40”

据我所知(不是很远),我得到的 JSON 是有效的,所以我不知道为什么会这样。我的代码也编译得很好……这里是:

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];
4

1 回答 1

9

错误消息的重要部分是:

-[__NSCFString JSONValue]: unrecognized selector sent to instance 0x6ab7a40

该类__NSCFStringNSString接口的私有实现类,所以你可以假装它说NSString.

因此,我们看到您正在将JSONValue消息发送到NSString,并且NSString表示它无法识别该选择器。SBJson 库使用 category向类添加一个JSONValue方法。NSString

所以我推断你没有链接NSObject+SBJson.o到你的应用程序。如果您将 SBJson 源文件复制到您的应用程序中,请确保您复制到 中NSObject+SBJson.m,并确保它包含在目标的“编译源”构建阶段。

如果您构建了一个 SBJson 库并将您的应用程序链接到该库,您可能需要将-ObjC标志添加到您的链接器选项,甚至是-all_load标志。

于 2012-02-23T20:08:39.910 回答