1

I'm new to Objective C. I'm experiencing issues whilst creating a unit conversion app. Here is the offending code (P.S I'm aware that isn't the correct conversion):

The problem is that when I press the convert button, it returns with the initialisation value of 0. I then have to press the button again to get my calculated value. Thank you.

Of course I'm a hugeee noob, this is my first day :).

-(IBAction)convert:(id)sender{

int unitType;
double unitTemp1 = [_tempField.text doubleValue];
double unitTemp2 = 0;

switch(unitType)
{
    case 1:
        //Celcius to Farenheight
        NSLog(@"Celcius to Farenheight Selected");
        unitTemp2 = unitTemp1*9;
    break;
}

if([_array objectAtIndex:1])
{
    unitType = 1;
}


NSString* resultString = [[NSString alloc] initWithFormat:@"%f",unitTemp2];
_tempResult.text = resultString;
//initwithformat
}
JamesWard
  • 13
  • 2
  • When this method executes, unitType is not initialised, and can be any random value. So no telling how your switch() statement will behave, Always initialise your variables in Obj C. – Greycon Aug 01 '14 at 15:48

2 Answers2

1

Its because you set unitType after the switch, solution is to move the code above, and provide a default to your switch

int unitType = 0;

if([_array objectAtIndex:1])
{
    unitType = 1;
}


switch(unitType)
{
    case 1:
        //Celcius to Farenheight
        NSLog(@"Celcius to Farenheight Selected");
        unitTemp2 = unitTemp1*9;
    break;
    default:
        //match anything else
        NSLog(@"Celcius to Farenheight not Selected");
    break;

}
meda
  • 43,711
  • 13
  • 85
  • 120
  • Way to edit your code after I put my answer @meda -- at least give me an upvote since you stole my thunder – pkatsourakis Aug 01 '14 at 15:59
  • @PanoKatsourakis bro why would you accuse me of stealing your answer, they are not even the same. the concept of this community is to help out, not be a rep-whhore – meda Aug 01 '14 at 16:03
  • We answered at almost the same time. Yours was way less detailed at that point. I left then you edited your answer so that it would be more likely to be chosen as correct. Who is the rep-hore here?? – pkatsourakis Aug 01 '14 at 16:04
  • 1
    well I am not the one begging for upvote, I posted before you and while I was editing you come make the same point and then complain smh. – meda Aug 01 '14 at 16:07
  • 1
    @PanoKatsourakis Meda's "answered Aug 1 '14 at 15:48" - Yours "answered Aug 1 '14 at 15:50" - it's clear that his was 2 minutes earlier. A lot of water can go under the bridge in 2 minutes ;-) Timestamps do not lie. – Funk Forty Niner Sep 14 '15 at 22:39
  • 1
    lol @Fred-ii- exactly bro, crazy things happen on this site haha – meda Sep 15 '15 at 18:24
  • 1
    @meda - and crazier things will continue to happen - *Cheers* ;-) I hope you are well bro. – Funk Forty Niner Sep 15 '15 at 18:24
  • 1
    lol je suis un man, j'espere que toi aussi je t'ai appeler bro a quelque reprise now, ok removing them now :P – meda Sep 15 '15 at 18:48
0

Try moving your if statement up so it is initialized to a value before your switch statement

int unitType;

if([_array objectAtIndex:1])
{
    unitType = 1;
} else
{ 
    unitType = 0;
}
pkatsourakis
  • 742
  • 3
  • 16