ユーザーの現在地を取得する際の、リバースジオコーディングの解決策について、セカンドオピニオンをお願いします。
- (void)reverseGeocodeLocation:(CLLocation *)location
{
CLGeocoder* reverseGeocoder = [[CLGeocoder alloc] init];
if (reverseGeocoder) {
[reverseGeocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark* placemark = [placemarks firstObject];
if (placemark && [placemark count] > 0) {
//Using blocks, get zip code
NSString *zipCode = [placemark.addressDictionary objectForKey:(NSString*)kABPersonAddressZIPKey];
}
}];
}
else{
MKReverseGeocoder* revGeo = [[MKReverseGeocoder alloc] initWithCoordinate:location.coordinate];
revGeo.delegate = self;//using delegate
[revGeo start];
[revGeo release];
}
[reverseGeocoder release];
}
しかし、ちょっとした問題があるようでした…。
[reverseGeocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
CLPlacemark* placemark = ...
}];
何が悪かったのか教えてください。私は受け取った EXC_BAD_ACCESS
エラー。
回答:
回答№1は1ブロックに送信されたエラーをチェックしていません。 少なくとも1つの目印が返されることを前提としていますが、配列が何らかの理由で空になっている可能性があります。それが確かにあなたのEXC_BAD_ACCESSエラーの原因となる可能性があります。