【发布时间】:2012-06-13 15:26:12
【问题描述】:
我正在尝试使用 iOS5 的 NSJSONSerialization 解析 JSON。这段代码第一次正确地解析了我想要的数据,但是数据只是保持不变。 URL 上的 JSON 已经改变,但代码一直给我它解析的第一个数据,现在是不正确的。不管我“构建和运行”多少次,它总是给我同样的东西。
当我将代码复制到新项目时,它再次工作,第一次,然后做同样的事情。
我不知道问题出在哪里,也许是缓存?
谢谢你的帮助!!!
- (void)fetchedData:(NSData *)responseData {
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData
options:kNilOptions
error:&error];
NSArray* allDepartures = [json objectForKey:@"departures"];
NSLog(@"departures: %@", allDepartures);
NSDictionary* stops = [allDepartures objectAtIndex:0];
NSNumber* time = [stops objectForKey:@"expected_mins"];
NSString* name = [stops objectForKey:@"headsign"];
nameLabel.text = [NSString stringWithFormat:@"%@",name];
timeLabel.text = [NSString stringWithFormat:@"%i",[time intValue]];
}
- (IBAction)getInfo:(id)sender {
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: myURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
【问题讨论】:
-
一段时间后(比如一小时),它会像第一次安装应用程序时一样加载(数据正确),然后它会做同样的事情,而不是更新
标签: iphone objective-c ios json nsjsonserialization