Using nested if statements to select textfield for update of associated label So I have written a very simple program using a cocoa pods library to calculate the distance between different cities. Here is what the UI looks like:



So I have a textfield on top for entry of starting location, and then four other textfields for destination city input.

I went with nested if statements which I am not fully happy with, but at least it does the job in terms of calculating distances for each. Is there a better way to do this than nesting if statements as I have done? There are only four cases for the four textfields but if there is a cleaner way to do it I would love to know about that.

Here is the code itself for the ViewController.m file:

#import "ViewController.h"
#import "DistanceGetter/DGDistanceRequest.h"

@interface ViewController ()

@property (nonatomic) DGDistanceRequest *req;

@property (weak, nonatomic) IBOutlet UITextField *startLocation;

@property (weak, nonatomic) IBOutlet UITextField *endLocationA;

@property (weak, nonatomic) IBOutlet UITextField *endLocationB;
@property (weak, nonatomic) IBOutlet UITextField *endLocationC;

@property (weak, nonatomic) IBOutlet UITextField *endLocationD;


@property (weak, nonatomic) IBOutlet UILabel *distanceA;
@property (weak, nonatomic) IBOutlet UILabel *distanceB;

@property (weak, nonatomic) IBOutlet UILabel *distanceC;
@property (weak, nonatomic) IBOutlet UILabel *distanceD;

@property (weak, nonatomic) IBOutlet UIButton *calculateButton;




@end


@implementation ViewController

- (IBAction)calculateButtonTapped:(id)sender {

    self.calculateButton.enabled = NO;

    self.req = [DGDistanceRequest alloc];
    NSString *start = self.startLocation.text;
    NSString *destA = self.endLocationA.text;
    NSString *destB = self.endLocationB.text;
    NSString *destC = self.endLocationC.text;
    NSString *destD = self.endLocationD.text;


    NSArray *dests = @[destA, destB, destC, destD];


    self.req = [self.req initWithLocationDescriptions:dests sourceDescription:start];
    __weak ViewController *weakSelf = self;


    self.req.callback = ^void(NSArray *responses){
        ViewController *strongSelf = weakSelf;
        if (!strongSelf) return;

        NSNull *badResult = [NSNull null];




          if (responses[0] != badResult) {

            double num = ([responses[0] floatValue] / 1000.0);
            NSString *ans1 = [NSString stringWithFormat:@"%.2f km", num];

            strongSelf.distanceA.text = ans1;

            if (responses[1] != badResult) {
                double num = ([responses[1] floatValue] / 1000.0);
                NSString *ans2 = [NSString stringWithFormat:@"%.2f km", num];

                strongSelf.distanceB.text = ans2;

                if (responses[2] != badResult) {
                    double num = ([responses[2] floatValue] / 1000.0);
                    NSString *ans3 = [NSString stringWithFormat:@"%.2f km", num];

                    strongSelf.distanceC.text = ans3;

                    if (responses[3] != badResult) {
                        double num = ([responses[3] floatValue] / 1000.0);
                        NSString *ans4 = [NSString stringWithFormat:@"%.2f km", num];

                        strongSelf.distanceD.text = ans4;


                    }



                }




            }



        }


        strongSelf.req = nil;
        strongSelf.calculateButton.enabled = YES;



    };

    [self.req start];



   }



@end


Please be patient with me as I am pretty much completely new to writing my own code from scratch.
First thing:


When you have a structure like that:

if (responses[1] != badResult) {
...
   if (responses[2] != badResult) {
   ...
      if (responses[3] != badResult) {
      ...


You probably have to use a loop, as the number of if statement will increase in function of the number of results in your array.

Also, you try to access to an array value without checking if this index is inside the size of your array:

i.e. : if (index >= 0 AND index &lt; responses.cout )

What if your array has less than 4 values?

So to prevent this, it will look like that:

for (int i = 0; i &lt; responses.count; i++) {
    if (responses[i] != badResult) {
        double num = ([responses[i] floatValue] / 1000.0);
        NSString *currentAnswer = [NSString stringWithFormat:@"%.2f km", num];
        // Do something with 'currentAnswer' like putting it inside an array (datasource)
    }
}



Second Thing


When you have that kind of UI (repetitive stuff, block/pattern of component with common function) 

=> it's always depending on context, 

But, it's better to use a component with reusable view, datasource: UITableView, UICollectionView etc...

You can check this tutorial if you need help to build a table view.

/------------------------------------/ 
          For testing only
/------------------------------------/

Also, there is another way, which is quick and dirty, but it works:

You can use view tag to access them in an ordinate way.


From your storyboard, set the tag value of the first distance label (distanceA) to 100
for distanceB 101
for distanceC 102


and so on...

Now you can access to the label from an integer thanks to 

UILabel *label = (UILabel*)[self.view viewWithTag:100];


So if you put it in the previous loop:

 for (int i = 0; i &lt; responses.count; i++) {
    if (responses[i] != badResult) {
        double num = ([responses[i] floatValue] / 1000.0);
        NSString *currentAnswer = [NSString stringWithFormat:@"%.2f km", num];
        // Getting the label with tag, THIS IS WEAK CODE
        UILabel *label = (UILabel*)[self.view viewWithTag:100+i];
        // set the currentAnswer string
        label.text = currentAnswer;
    }
}


/-------------------------------------------------/

Remember, using UITableView or UICollectionView is much better!