我想知道如何避免所有嵌套的if/success塊?所以,我很高興與任何建議,就如何改善/封裝這個架構Clean代碼:如何避免所有嵌套的if/success塊?
- (void)findLocation {
// Show loader.png
[self.searchButton setImage:[UIImage imageNamed:@"loader"] forState:UIControlStateNormal];
// Request url
NSString *url = [NSString stringWithFormat:@"http://api.domain.com/someendpoint/"];
// Send http request
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager GET:url parameters:nil
success:^(AFHTTPRequestOperation *operation, id responseObject) {
// If API returned error
if([responseObject objectForKey:@"error"]) {
// Show an error
// Hide loader
[self.searchButton setImage:[UIImage imageNamed:@"some_button"] forState:UIControlStateNormal];
// Skip everything else
return;
}
// If API returned data
// Init location object
Location * location = [[Location alloc] init];
location.name = [responseObject objectForKey:@"name"];
location.image_url = [responseObject objectForKey:@"image_url"];
location.url = [responseObject objectForKey:@"url"];
location.address = [responseObject objectForKey:@"address"];
// Calculate long/lat from address (API does not provide this information
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:location.address completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
// Show error
} else {
// Save geo-location to location object
CLPlacemark *place = [placemarks lastObject];
location.location = CLLocationCoordinate2DMake(place.location.coordinate.latitude, place.location.coordinate.longitude);
// Present ResultViewController
ResultViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"ResultView"];
controller.location = location;
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:controller animated:YES completion:nil];
// Initialize region to monitor
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:location.location radius:150.0 identifier:[[NSUUID UUID] UUIDString]];
// Start monitoring new region
[self.locationManager startMonitoringForRegion:region];
// Stop UpdatingLocation
[self.locationManager stopUpdatingLocation];
// Hide loader
[self.searchButton setImage:[UIImage imageNamed:@"some_button"] forState:UIControlStateNormal];
}];
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
// Show error
// Hide loader
[self.searchButton setImage:[UIImage imageNamed:@"some_button"] forState:UIControlStateNormal];
}
];
}
完成。還有什麼? – fabian