我非常新的iOS開發,請與我裸露=]地理編碼和設置全局變量的iOS
我有兩個全局的NSString變量稱爲myLocality和myCountry ...
我已經設置了地理編碼爲如下:
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
finishedGettingLocation = false;
if (newLocation.horizontalAccuracy < 200.0f){
[locationManager stopUpdatingLocation];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
__block NSString *locality;
__block NSString *country;
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks) {
locality = placemark.locality;
country = placemark.ISOcountryCode;
}
}];
do {
} while ([geoCoder isGeocoding]);
myLocality = locality;
myCountry = country;
finishedGettingLocation = true;
}
}
我的後臺線程如下:
- (void) doWork
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
finishedGettingLocation = false;
dispatch_async(dispatch_get_main_queue(), ^{
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyHundredMeters];
[locationManager startUpdatingLocation];
});
int counter = 0;
do {
[NSThread sleepForTimeInterval:.1];
counter ++;
if (counter % 200 == 0){
dispatch_async(dispatch_get_main_queue(), ^{
Toast *oToast = [Toast toastWithMessage:@"Gps pin-pointing works faster if you are outside"];
[oToast showOnView:self.view];
});
counter = 0;
}
} while (!finishedGettingLocation);
NSLog(@"%@", myCountry); // this is where I am trying to print
NSLog(@"%@", myLocality); // this is where I am trying to print
});
}
(是的,我知道這是相當草率的代碼,但是,.. xD)
現在,我不明白爲什麼這些返回null時,我將它們記錄在我的代碼在其他地方。 但是,當我把上面的for
登錄,即NSLog(@"%@", placemark.locality)
我得到正確的值,或者至少我得到一個值。
如何設置placemark
的全局變量?
你在哪裏試圖打印出它們的值? doWork方法?我會嘗試通過這兩種方法來看看發生了什麼。 – canhazbits