2014-07-14 43 views
-2

Hai我正在代碼塊中寫代碼來獲取車輛的地標。我的問題是我將地標存儲在NSMutablearray內,但我無法訪問塊外的數組。請好心勸我解決這個問題。提前致謝。我的代碼如下...不能使用塊外的數組

 CLLocation *someLocation=[[CLLocation alloc]initWithLatitude:latitude longitude:longitude]; 
     CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
     [geocoder reverseGeocodeLocation:someLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
      if ([placemarks count] > 0) { 
       NSDictionary *dictionary = [[placemarks objectAtIndex:0] addressDictionary]; 
       addressOutlet=[dictionary valueForKey:@"Street"]; 
       City=[dictionary valueForKey:@"City"]; 
       State=[dictionary valueForKey:@"State"]; 
       if (addressOutlet!=NULL&&City!=NULL) 
       { 
        SubTitle=[NSString stringWithFormat:@"%@,%@,%@",addressOutlet,City,State]; 
       } 
       else if (addressOutlet==NULL&&City!=NULL) 
       { 
        SubTitle=[NSString stringWithFormat:@"%@,%@",City,State]; 
       } 
       else if (addressOutlet!=NULL&&City==NULL) 
       { 
        SubTitle=[NSString stringWithFormat:@"%@,%@",addressOutlet,State]; 
       } 
       else if(addressOutlet==NULL&&City==NULL&&State!=NULL) 
       { 
        SubTitle=[NSString stringWithFormat:@"%@",State]; 
       } 
       else if (addressOutlet==NULL&&City==NULL&&State==NULL) 
       { 
        SubTitle=[NSString stringWithFormat:@"%@",@""]; 
       } 
       [storeArray addObject:SubTitle]; 
      } 
      NSLog(@"%@",storeArray);// I can access here 
     }]; 

    NSLog(@"%@",storeArray);// Here it shows empty array 
} 
+0

這是因爲打印空數組的NSLog行在分組代碼完成之前運行。如果您將塊內部的storeArray傳遞給打印它的函數,它將運行正常。 – PallakG

+0

謝謝。你能指導我一些示例代碼嗎? – iBeginner

+0

完成塊在不同的時間運行_usually_,而不是在_defined_之後依次運行。因此您的代碼在完成塊之外運行_before_實際完成塊_will_。 (埃米特L.布朗:_「你只是沒有想到第四維!」)_ – holex

回答

0

這是由於塊。

您收集的數組是在過程發生一段時間後執行的異步。

但外日誌中獲取數據的完成,並收集之前執行的,因此它沒有返回值,不打印任何東西

[geocoder reverseGeocodeLocation:someLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
      //Some process collecting values in array 
       [storeArray addObject:SubTitle]; 
      } 
      NSLog(@"%@",storeArray);// I can access here 
      //Do everything here after data gets populated 
      [tableview reloadData]; 
     }]; 
//Here no values exist since execuion of this happens before the inner block execution 
} 
+0

謝謝。你能指導我一些示例代碼嗎? – iBeginner

+0

我需要將數組值傳遞給UITableView。 – iBeginner

+0

因此,調用tableview重新加載數據塊內的數據......它將工作 –

0

,由於異步執行reverseGeocodeLocation:completionHandler方法是,在你的情況下,第二NSLog會先調用,然後是第一個完成塊。

檢查:Obj-C blocks

--------- -----------編輯

您必須繼續完成塊的邏輯(即當你打電話NSLog在第一時間)。

-(void)downloadLocations:(void(^)(void))callback 
{ 
    storeArray = [[NSMutableArray alloc] init]; 
    //... 
    for(int f=0;f<Vehicle_No.count;f++){ 
     //... 
     CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
     [geocoder reverseGeocodeLocation:someLocation completionHandler:^(NSArray *placemarks, NSError *error) { 
      //... 
      NSLog(@"%@",storeArray);// I can access here 

      if (callback) { 
       callback(); 
      } 
     }]; 
    }   
} 

- (void)foo 
{ 
    //... 
    [self downloadLocations:^{ 
     //do something here with storeArray 

    }]; 
} 
+0

謝謝。你能指導我一些示例代碼嗎? – iBeginner

+0

It crases ...... – iBeginner

+0

在哪一行? 崩潰日誌? – arturdev

3

你應該知道的是執行流程。

在古代程序中,代碼執行總是從上到下飛行。無論如何,這個前提在很久以前就破裂了。現代程序由幾個組件構成,如函數,類和塊(閉包),程序並不總是從上到下。 (好吧,雖然它大部分都是這樣)

塊(閉包)是這個非順序程序之一。將代碼塊保存到變量中以稍後執行。問題是,代碼塊在被定義時沒有被執行。

你在這裏做什麼是:

  1. 做一個空數組。
  2. 創建並傳遞將數組填充到方法中的代碼塊。
  3. 使用NSLog打印陣列。

在#2點,代碼塊不會立即執行,但會在操作完成時執行。然後,當您打印數組時,該數組尚未填充,然後它只打印一個空數組。

您應該看待在#2定義的代碼塊將在未知的未來時間點執行,而且您無法控制時間。這是異步編程的一個難點,您需要熟悉一些內容。