views:

163

answers:

1

I'm new to iPhone and Objective C programming but I'm able to make my code work. I'm just not sure if it's the best way to do it because it requires a lot of code and doesn't look pretty.

I have a UIPickerView wheel that has 5 components, 120 multidimensional arrays (each with 13 rows and 7 columns) that I placed right in the "DidSelectRow" method.

My two questions are:

1) Should I not place all these arrays in the method but instead learn to place them in a separate file, perhaps an SQL database? Like I said, the pickerview works fine, it's just not pretty coding for one, and perhaps it's not letting my app run as efficiently as possible.

2) With all the components and arrays in my UIPickerView, the only way I was able to get my data from the arrays to properly display when a certain combination of pickerwheels is selected was to write A LOT of "if" and "else if" statements. It looks ridiculous and there must be a better way! I couldn't figure out if I could use or how to use a Switch statement for my method. I've included some sample code if this helps anyone understand what I'm trying to do.

Thanks for the help in advance!

if (pressAltWheel == 0 && antiIceWheel == 0 && thrustWheel == 0)
    {
        {
            //4600ft
            if (tempWheel == 9 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[0][0]];

        else if (tempWheel == 11 && runwayLengthWheel == 0)
                weightValue.text = [NSString stringWithFormat:@"%@",column0[1][0]];

        else if (tempWheel == 13 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[2][0]];

        else if (tempWheel == 15 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[3][0]];

        else if (tempWheel == 16 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[4][0]];

        else if (tempWheel == 17 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[5][0]];

        else if (tempWheel == 18 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[6][0]];

        else if (tempWheel == 19 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[7][0]];

        else if (tempWheel == 20 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[8][0]];

        else if (tempWheel == 21 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[9][0]];

        else if (tempWheel == 22 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[10][0]];

        else if (tempWheel == 23 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[11][0]];

        else if (tempWheel == 24 && runwayLengthWheel == 0)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[12][0]];
        }

        //5300ft
        {


        if (tempWheel == 9 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[0][1]];

        else if (tempWheel == 11 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[1][1]];

        else if (tempWheel == 13 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[2][1]];

        else if (tempWheel == 15 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[3][1]];

        else if (tempWheel == 16 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[4][1]];

        else if (tempWheel == 17 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[5][1]];

        else if (tempWheel == 18 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[6][1]];

        else if (tempWheel == 19 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[7][1]];

        else if (tempWheel == 20 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[8][1]];

        else if (tempWheel == 21 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[9][1]];

        else if (tempWheel == 22 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[10][1]];

        else if (tempWheel == 23 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[11][1]];

        else if (tempWheel == 24 && runwayLengthWheel == 1)
            weightValue.text = [NSString stringWithFormat:@"%@",column0[12][1]];
    }
A: 

Why not optimise the code somehow to be like this:

if (tempWheel >= 15)
    weightValue.text = [NSString stringWithFormat:@"%@",column0[tempWheel-12][runwayLengthWheel]];

else
    weightValue.text = [NSString stringWithFormat:@"%@",column0[(tempWheel-9)/2][runwayLengthWheel]];

This is a lot cleaner :)

Adam Woś
Wow! That's great, thanks! I tried just that much code and you're right, it is a lot cleaner and works. Could I ask you to explain it a bit to me if you have the time? I'm not understanding the logic in this and I'd like to continue using this format. Thanks again!
Spottswoode
Nevermind, I figured out what's going on now... very clever, thanks again!
Spottswoode
Glad you figured it out :) I would appreciate if you could mark my response as an accepted answer, though, so that I could get some points :)
Adam Woś
Just saw this now... sorry, here ya go!
Spottswoode