开发者

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(@”开发者_JS百科s now = %f”, s);

}

Help much appreciated!


- (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.


- (IBAction)calculate:(UIButton *)button {
    float s;
    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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜