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
}
這是因爲打印空數組的NSLog行在分組代碼完成之前運行。如果您將塊內部的storeArray傳遞給打印它的函數,它將運行正常。 – PallakG
謝謝。你能指導我一些示例代碼嗎? – iBeginner
完成塊在不同的時間運行_usually_,而不是在_defined_之後依次運行。因此您的代碼在完成塊之外運行_before_實際完成塊_will_。 (埃米特L.布朗:_「你只是沒有想到第四維!」)_ – holex