我想从 Xcode (Objective-C) 中的 JSON 获取值。但是,如果我更改该值并再次构建该应用程序,该值不会更改。
我试过的是
将值设置为“1”并构建应用程序(第一次)-> 我可以得到“1”。
将值从“1”设置为“0”并构建应用程序(第二次)-> 我仍然可以得到“1”。
从 iPhone 删除该应用程序并再次构建该应用程序 -> 我可以得到“0”。
为什么我不能从 JSON 获取最新值?
编码:
-(int)getJsonValue{
Reachability* curReach = [Reachability reachabilityForInternetConnection];
NetworkStatus netStatus = [curReach currentReachabilityStatus];
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
if (netStatus == NotReachable) {
// Disconnect
_jsonValue = 0;
} else {
// Connect (Always through here)
NSURL* url = [NSURL URLWithString:pathReview];
NSURLSessionConfiguration* config = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession* session = [NSURLSession sessionWithConfiguration:config];
NSURLSessionDataTask* task =
[session dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
id jsonData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if ([jsonData isKindOfClass:[NSArray class]]){
NSMutableArray *objects = [NSMutableArray array];
objects = jsonData;
_jsonValue = [[objects objectAtIndex:0] intValue];
}
dispatch_semaphore_signal(semaphore);
}];
[task resume];
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
}
return _jsonValue;
}
您需要通过块返回值,因为您在函数内部使用了块。
-(void)getJsonValueWithCallback:(void(^)(NSInteger jsonValue ))callback{
if (netStatus == NotReachable) {
callback(0);
}
else{
callback(1);
}
}
打电话
[self getJsonValueWithCallback:^(NSInteger jsonValue) {
NSLog(@"%ld",(long)jsonValue);
//
}];
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句