2017-06-02 47 views
0

我想從Xcode中獲取JSON的值(Objective-C)。 但是,如果我更改值並再次構建應用程序,則值不會更改。無法從Xcode中獲取JSON的最新值

我想什麼是

  1. 將值設置爲「1」和構建應用程序(在第一時間) - >我可以得到「1」。

  2. 將值從「1」設置爲「0」並構建應用程序(第二次) - >我仍然可以獲得「1」。

  3. 從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; 
} 
+0

你在哪裏託管你拉什麼文件?你正在更新文件,然後立即嘗試應用程序?這可能是您所託管文件的服務器出現問題,但實際上並未提供您期望的更新文件。我在Amazon S3上看到過類似的問題,它可以在實際提供新文件之前保存緩存文件 – zfetters

+0

我使用Coda更改了值並在設備上立即檢查了值並在24之後小時。但還沒有改變。在另一個應用程序中,我可以立即獲取最新值。 – bao

+0

您在使用塊內部方法時無法返回值。你需要用block聲明方法。 – KKRocks

回答

0

您需要通過塊因爲使用了內部功能塊的返回值。

-(void)getJsonValueWithCallback:(void(^)(NSInteger jsonValue))callback{ 
    if (netStatus == NotReachable) { 
     callback(0); 
    } 
    else{ 
     callback(1); 
    } 
} 

調用

[self getJsonValueWithCallback:^(NSInteger jsonValue) { 
     NSLog(@"%ld",(long)jsonValue); 
     // 
    }];