我觉得我的问题实际上是阻塞,但也许还有其他问题。我正在尝试转发地址的地址解析并将坐标放入数组中以供以后使用。
当我尝试调用我尝试添加到块中数组中的对象之一时,底部会引发异常。在任何NSLogs在块文本中打印出来之前,也会引发该异常。
处理此问题的正确方法是什么?谢谢。
- (NSMutableArray *)convertAddressToGeocode:(NSString *)addressString
{
//return array with lat/lng
__block NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:0];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:addressString
completionHandler:^ (NSArray* placemarks, NSError* error) {
for (CLPlacemark* aPlacemark in placemarks)
{
// Process the placemark.
if (error){
NSLog(@"Geocode failed with error: %@", error);
[self displayError:error];
return;
}
else {
NSArray const *keys = @[@"coordinate.latitude",
@"coordinate.longitude",
@"altitude",
@"horizontalAccuracy",
@"verticalAccuracy",
@"course",
@"speed",
@"timestamp"];
NSString *keylat = keys[0];
NSString *keylng = keys[1];
if (aPlacemark.location == nil)
{
NSLog(@"location is nil.");
}
else if ([keylat isEqualToString:@"coordinate.latitude"] && [keylng isEqualToString:@"coordinate.longitude"])
{
NSString *lat = @"";
NSString *lng = @"";
lat = [self displayStringForDouble: [aPlacemark.location coordinate].latitude];
lng = [self displayStringForDouble: [aPlacemark.location coordinate].longitude];
NSLog(@"This never gets executed"): //THIS NEVER GETS EXECUTED
[coordinates addObject:[NSString stringWithFormat:@"%@",lat]];
[coordinates addObject:[NSString stringWithFormat:@"%@",lng]];
}}}}];
NSLog(@"Array: %@", coordinates[0]); //EXCEPTION RAISED HERE, Nothing ever gets added
return coordinates;
}
这是此方法应该插入的代码,但我没有从convertAddresstoGeocode中获取坐标以传递给convertCoordinatestoRepModel:
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *addressToSearch = self.addressSearchField.text;
NSMutableArray *coordinateArray = [self convertAddressToGeocode:addressToSearch];
NSMutableArray *repModelArray = [self convertCoordinatesToRepModel:coordinateArray];
...
}
如果geocodeAddressString
是异步操作,则将在执行之后执行您的块
NSLog(@"Array: %@", coordinates[0]);
同样,在方法调用结束后(事件已处理时),坐标数组被释放(这是由于__block修饰符-块不保留带有__block修饰符的对象),并且在您的块中尝试使用已分配的coordinates
数组。
再次:
您的街区将在之后被调用 NSLog(@"Array: %@", coordinates[0]);
fe:
NSLog(@"Array: %@", coordinates[0]);
-在那一刻,数组为空是正常的。coordinates
数组存储在一些数组中@property
,可以在in块中使用后释放它更新:
在.h文件中
typedef void (^ConverteArrayCallback)(NSArray *coordinates);
在下面 @intrerface
- (void)convertAddressToGeocode:(NSString *)addressString callback:(ConverteArrayCallback) callback;
在.m文件中
- (void)convertAddressToGeocode:(NSString *)addressString callback:(ConverteArrayCallback) callback {
NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:0];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:addressString
completionHandler:^ (NSArray* placemarks, NSError* error) {
for (CLPlacemark* aPlacemark in placemarks)
{
// Process the placemark.
if (error){
NSLog(@"Geocode failed with error: %@", error);
[self displayError:error];
return;
}
else {
NSArray const *keys = @[@"coordinate.latitude",
@"coordinate.longitude",
@"altitude",
@"horizontalAccuracy",
@"verticalAccuracy",
@"course",
@"speed",
@"timestamp"];
NSString *keylat = keys[0];
NSString *keylng = keys[1];
if (aPlacemark.location == nil)
{
NSLog(@"location is nil.");
}
else if ([keylat isEqualToString:@"coordinate.latitude"] && [keylng isEqualToString:@"coordinate.longitude"])
{
NSString *lat = @"";
NSString *lng = @"";
lat = [self displayStringForDouble: [aPlacemark.location coordinate].latitude];
lng = [self displayStringForDouble: [aPlacemark.location coordinate].longitude];
NSLog(@"This never gets executed"): //THIS NEVER GETS EXECUTED
[coordinates addObject:[NSString stringWithFormat:@"%@",lat]];
[coordinates addObject:[NSString s
tringWithFormat:@"%@",lng]];
}}}
if (callback != NULL) {
callback(coordinates);
}
}];
}
那应该有用!
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *addressToSearch = self.addressSearchField.text;
[self convertAddressToGeocode:addressToSearch callback:^(NSArray *coordinates)
{
self.textView.text = [coordinates objectAtIndex:0];
}];
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句