我正在使用单例类(通过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期间,字符串是否会以某种方式损坏?
最佳答案
弄清楚了...我在项目中包含了JSONKIt.h,但是由于某些奇怪的原因,JSONKit.m没有包含在“构建阶段”下的“编译源”中-手动添加后,它就可以正常工作。