Search code examples
iphoneobjective-cvariablesscoping

Really really basic Obj-C question about passing instance variables


Hi sorry about such a dumb question I am changing a variable value depending on the index of a segmented control but then want to use this variable in a calculation that follows; am sure this has something to do with variable scoping?

- (IBAction)calculate:(UIButton *)button {
if( [sSeg selectedSegmentIndex]==1){
    float  s=0.5;
    NSLog(@"s=%f", s);
}
else if ([sSeg selectedSegmentIndex]==0)
{
    float s=1; 
    NSLog(@"s=%f", s);
}
NSLog(@”s now = %f”, s);

}

Help much appreciated!


Solution

  • - (IBAction)calculate:(UIButton *)button {
        float s = 0;
        if( [sSeg selectedSegmentIndex]==1){
            s=0.5;
            NSLog(@"s=%f", s);
        }
        else if ([sSeg selectedSegmentIndex]==0)
        {
            s=1; 
            NSLog(@"s=%f", s);
        }
        NSLog(@”s now = %f”, s);
    

    }

    Yeah, its the scope - a variable is only visible inside your curly brackets.