2014-01-28 88 views
0

我想知道如何避免所有嵌套的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]; 

     } 
    ]; 
} 

回答

0

Show error部分後,從函數返回。這樣你可以將其餘的代碼放在else子句之外。

+0

完成。還有什麼? – fabian

0

正如S. McConnel所建議的,如果處理成功的場景,首先使用 - 這使得代碼讀取更容易。

根據zmbq的建議,將其包裝到一個函數中。

塊中的obj c是不是真的在「前10名」爲自己的美麗,只得到與他們:)使用

從谷歌的一些技巧對目標C代碼風格:

http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml#Blocks

作爲您的功能變體:

- (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 data 
     if([[responseObject objectForKey:@"ErrorCode"] intValue] == 0) { 

      [self setupLocation:responseObject]; 

     // If API returned error  
     } 
     else { // Show an error 

       // Hide loader 
      [self.searchButton setImage:[UIImage imageNamed:@"surpriseme_button"] forState:UIControlStateNormal]; 
       } 
      }]; 
     } 
    } 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

     // Hide loader 
     [self.searchButton setImage:[UIImage imageNamed:@"surpriseme_button"] forState:UIControlStateNormal]; 

    } 
]; 
}