关于传递实例变量的非常非常基本的 Obj-C 问题

发布于 2024-10-06 17:34:30 字数 356 浏览 1 评论 0原文

嗨,很抱歉问了这么愚蠢的问题 我正在根据分段控件的索引更改变量值,但随后希望在接下来的计算中使用该变量;我确定这与变量作用域有关吗?

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

非常感谢帮助

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱猫软卧 2024-10-13 17:34:31
- (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);

是的

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

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

合久必婚 2024-10-13 17:34:31
- (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);
}
- (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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文