CLGeocoderを使って、ランダムに出力した緯度・経度から国名を取得するべく、
以下のプログラムで
国名をcountry
に代入したいのですが、うまくいきません。
[geocoder reverseGeocodeLocation:location ...] のカッコの外では取得できているのですが、カッコ外で使用したいです。
objective-cの基本的な考え方が理解できてないような質問だと思いますが、はまってしまって抜け出ることができず、ご教授いただければ幸いです。
#Viewcontroller.m
C
1 2#import <CoreLocation/CoreLocation.h> 3 4NSString country,ken,city; 5 6- (void)viewDidAppear:(BOOL)animated 7{ 8//緯度乱数 9 int min = 0, max = 90*1000000; 10 lati = ((float)arc4random_uniform(max + 1 - min) + min) /1000000; 11//経度乱数 12 min = 0, max = 180*1000000; 13 longi = ((float)arc4random_uniform(max + 1 - min) + min) /1000000; 14 15 CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 16 17 CLLocation *location = [[CLLocation alloc] initWithLatitude:lati longitude:longi]; 18 [geocoder reverseGeocodeLocation:location 19 completionHandler:^(NSArray* placemarks, NSError* error) { 20 if(error){ 21 NSLog(@"エラー %@", error); 22 } else { 23 if ([placemarks count] > 0) { 24 CLPlacemark *placemark = (CLPlacemark *)[placemarks lastObject]; 25 country = placemark.country; 26 NSLog(@"%@", country);//ここでは国名が表示されます 27 28 } 29 } 30 }]; 31 32 NSLog(@"%@", country); //ここでは国名が表示されません 33} 34
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/01 12:33
2016/09/01 13:00