关于传递实例变量真的​​很基本的Obj-C问题

时间:2010-12-09 01:32:49

标签: iphone objective-c variables 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);

}

非常感谢!

2 个答案:

答案 0 :(得分:4)

- (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);

}

是的,它的范围 - 变量只在大括号内可见。

答案 1 :(得分:0)

- (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);
}