我正在使用单例类从远程服务器(通过NSURLConnection)获取JSON - 除非我尝试使用JSONKit解析JSON,否则一切看起来都很好。
这是一些代码
-(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];
}
我得到的错误是在这一行
NSDictionary *deserializedData = [jsonData objectFromJSONString];
-[__NSCFString objectFromJSONString]: unrecognized selector sent to instance 0x7fc1cd0
有什么想法在这里发生了什么?这似乎是使用JsonKit解析JSON的常用方法。
我已经确定JSON是有效的......在附加到didReceiveResponse期间,字符串是否会以某种方式损坏?
答案 0 :(得分:8)
想出来......我在项目中包含了JSONKIt.h,但由于一些奇怪的原因,JSONKit.m没有被包含在'Build Phases'下的'Compile Sources'中 - 一旦我手动添加它就开始工作了细