不能从块添加对象到NSMutableArray

我有一种感觉,那就是我的问题真的是阻塞,但也许是另一回事。 我正在尝试将地址转换为地址,并将坐标放置到数组中以便稍后使用。

当我尝试调用我尝试添加到块中数组的对象之一时,会在底部产生exception。 在任何NSLogs在块文本内打印出来之前,exception也会被触发。

什么是正确的方法来处理这个? 谢谢。

- (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是asynchronous操作,那么您的块将在执行之后执行

 NSLog(@"Array: %@", coordinates[0]); 

另外,在你的方法调用结束后(当事件已经处理完毕)释放的坐标数组(这是因为__block修饰符 – 块不保留具有__block修饰符的对象),并在你的块中尝试使用交换coordinates数组。

再来一次:

您的块将在NSLog(@"Array: %@", coordinates[0]);后被调用NSLog(@"Array: %@", coordinates[0]);

FE:

  1. 删除NSLog(@"Array: %@", coordinates[0]); – 在那一刻arrays是空的是正常的。
  2. 将你的coordinates数组存储在某个@property ,你可以在块中使用后释放它

更新:

在.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]; }]; } 
 __block NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:0]; 

问题在这里; 只需将上面的代码replace为:

 NSMutableArray *coordinates = [[NSMutableArray alloc] init];