Impossible d'append AddObject à NSMutableArray à partir du bloc

J'ai le sentiment que mon problème ici est vraiment de bloquer, mais c'est peut-être autre chose aussi. J'essaye d'avancer une adresse de géocode et place les coordonnées dans un tableau pour employer plus tard.

Une exception est levée en bas lorsque j'essaie d'appeler l'un des objects que j'ai essayé d'append au tableau dans le bloc. L'exception est également levée avant que l'un des NSLogs ne s'imprime dans le text du bloc.

Quelle est la bonne façon de gérer cela? Merci.

- (NSMutableArray *)convertAddressToGeocode:(NSSsortingng *)addressSsortingng { //return array with lat/lng __block NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:0]; CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressSsortingng:addressSsortingng 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"]; NSSsortingng *keylat = keys[0]; NSSsortingng *keylng = keys[1]; if (aPlacemark.location == nil) { NSLog(@"location is nil."); } else if ([keylat isEqualToSsortingng:@"coordinate.latitude"] && [keylng isEqualToSsortingng:@"coordinate.longitude"]) { NSSsortingng *lat = @""; NSSsortingng *lng = @""; lat = [self displaySsortingngForDouble: [aPlacemark.location coordinate].latitude]; lng = [self displaySsortingngForDouble: [aPlacemark.location coordinate].longitude]; NSLog(@"This never gets executed"): //THIS NEVER GETS EXECUTED [coordinates addObject:[NSSsortingng ssortingngWithFormat:@"%@",lat]]; [coordinates addObject:[NSSsortingng ssortingngWithFormat:@"%@",lng]]; }}}}]; NSLog(@"Array: %@", coordinates[0]); //EXCEPTION RAISED HERE, Nothing ever gets added return coordinates; } 

Voici le code dans lequel cette méthode est censée être branchée, mais je ne reçois pas les coordonnées de convertAddresstoGeocode pour passer à convertCoordinatestoRepModel:

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { NSSsortingng *addressToSearch = self.addressSearchField.text; NSMutableArray *coordinateArray = [self convertAddressToGeocode:addressToSearch]; NSMutableArray *repModelArray = [self convertCoordinatesToRepModel:coordinateArray]; ... } 

si geocodeAddressSsortingng est une opération async que votre bloc sera effectué après

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

aussi, après l'appel de votre méthode se termine (lorsque l'événement est déjà géré) le tableau de coordonnées libéré (c'est à cause du modificateur __block – les blocs ne conservent pas les objects avec le modificateur __block) et dans votre bloc vous essayez d'utiliser le tableau des coordinates désalloué.

Encore une fois:

Votre bloc sera appelé après NSLog(@"Array: %@", coordinates[0]);

fe:

  1. Supprimer NSLog(@"Array: %@", coordinates[0]); – il est normal que dans ce moment le tableau soit vide.
  2. Stockez votre tableau de coordinates dans certains @property , vous pouvez le libérer après l'avoir utilisé en bloc

METTRE À JOUR:

dans le file .h

 typedef void (^ConverteArrayCallback)(NSArray *coordinates); 

sous @intrerface

 - (void)convertAddressToGeocode:(NSSsortingng *)addressSsortingng callback:(ConverteArrayCallback) callback; 

au format .m

 - (void)convertAddressToGeocode:(NSSsortingng *)addressSsortingng callback:(ConverteArrayCallback) callback { NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:0]; CLGeocoder *geocoder = [[CLGeocoder alloc] init]; [geocoder geocodeAddressSsortingng:addressSsortingng 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"]; NSSsortingng *keylat = keys[0]; NSSsortingng *keylng = keys[1]; if (aPlacemark.location == nil) { NSLog(@"location is nil."); } else if ([keylat isEqualToSsortingng:@"coordinate.latitude"] && [keylng isEqualToSsortingng:@"coordinate.longitude"]) { NSSsortingng *lat = @""; NSSsortingng *lng = @""; lat = [self displaySsortingngForDouble: [aPlacemark.location coordinate].latitude]; lng = [self displaySsortingngForDouble: [aPlacemark.location coordinate].longitude]; NSLog(@"This never gets executed"): //THIS NEVER GETS EXECUTED [coordinates addObject:[NSSsortingng ssortingngWithFormat:@"%@",lat]]; [coordinates addObject:[NSSsortingng s sortingngWithFormat:@"%@",lng]]; }}} if (callback != NULL) { callback(coordinates); } }]; } 

Cela devrait fonctionner!

 - (BOOL)textFieldShouldReturn:(UITextField *)textField { NSSsortingng *addressToSearch = self.addressSearchField.text; [self convertAddressToGeocode:addressToSearch callback:^(NSArray *coordinates) { self.textView.text = [coordinates objectAtIndex:0]; }]; } 
 __block NSMutableArray *coordinates = [[NSMutableArray alloc] initWithCapacity:0]; 

Le problème est ici; il suffit de replace le code ci-dessus par:

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