(objective-C)我可以在两个不同的函数中使用相同的变量名,但在同一个文件中吗?

时间:2022-11-25 16:55:09

Maybe this is implicit, but I am having doubts about it.

也许这是含蓄的,但我对此表示怀疑。

I have a .m file that has 2 different functions (or methods, as they are also called); but both functions uses a variable that I declare locally inside each function.

我有一个.m文件,它有两个不同的函数(或方法,它们也被称为);但两个函数都使用一个变量,我在每个函数中声明它。

For sake of clarity, I like to use the same name; and from what I understand; there should be no problem in having both functions having the same variable name, since they will point to 2 different pointer locations in memory, so it should not matter.

为了清楚起见,我喜欢使用相同的名称;根据我的理解;让两个函数具有相同的变量名应该没有问题,因为它们将指向内存中的两个不同的指针位置,所以这不重要。

Am I missing something or it is safe to do what I am doing? Should I use a different name? Should I use a global and change it accordingly, locally?

我是不是漏掉了什么东西,或者做我正在做的事情是安全的?我应该换个名字吗?我是否应该使用全局变量并相应地在本地进行更改?

-(int) doThis
{

    int pressure = 1;

    ...do the calculations and return the results...

}

-(int) doThat
{

    int pressure = 5;
    ...do the calculations and return the results....

}

1 个解决方案

#1


8  

Yes, certainly.

是的,当然可以。

You could even use the same variable name within different scopes in the same method.

您甚至可以在同一方法的不同作用域内使用相同的变量名。

Sample:

示例:

-(int) doThat
{

    int pressure = 5;

    for (int i = 0, i < pressure, i++) {
        int pressure = 10; // This is another variable!!! 
        pressure = 15;
        //Whatever
    }

    return pressure; //This would return 5!
}

I did that some times unintentionally. The errors that may result of that can be a pain in the a.. because they can be hard to see.

我无意中做了几次。这可能导致的错误可能是a的痛苦。因为它们很难看出来。

Just to add some more confustion: All within the same class, you could have an instance variable pressure which would hide a global variable pressure, if any. A method named pressure may have a parameter pressure which hides the instance variable. Then you may declare several variables pressure within different scopes within that method. All of them would hide their individually named counterparts in of the enclosing scope. And if you really want it confusing, then add a property named pressure to it. And that all may even work fine until you start loosing control or re-visit the same code a couple of weeks later.

再补充一点困惑:在同一个类中,你可以有一个实例变量压强,它会隐藏一个全局变量压强,如果有的话。命名为pressure的方法可能具有隐藏实例变量的参数压力。然后可以在该方法的不同作用域内声明多个变量压力。它们都将各自命名的副本隐藏在封闭范围内。如果你真的想让它混乱,那就添加一个属性,叫做压力。这一切甚至都可以正常工作,直到你开始失去控制或者几周后重新访问相同的代码。

Edit: the method pressure would clearly conflict with the getter of the property pressure. Meaning, it would work but the compiler would take the method as the getter given that it returns an object of the appropritate type. Or so.

编辑:方法压力将明显与属性压力的getter发生冲突。也就是说,它可以工作,但是编译器会把这个方法作为getter,因为它返回的对象是专用类型的对象。左右。

#1


8  

Yes, certainly.

是的,当然可以。

You could even use the same variable name within different scopes in the same method.

您甚至可以在同一方法的不同作用域内使用相同的变量名。

Sample:

示例:

-(int) doThat
{

    int pressure = 5;

    for (int i = 0, i < pressure, i++) {
        int pressure = 10; // This is another variable!!! 
        pressure = 15;
        //Whatever
    }

    return pressure; //This would return 5!
}

I did that some times unintentionally. The errors that may result of that can be a pain in the a.. because they can be hard to see.

我无意中做了几次。这可能导致的错误可能是a的痛苦。因为它们很难看出来。

Just to add some more confustion: All within the same class, you could have an instance variable pressure which would hide a global variable pressure, if any. A method named pressure may have a parameter pressure which hides the instance variable. Then you may declare several variables pressure within different scopes within that method. All of them would hide their individually named counterparts in of the enclosing scope. And if you really want it confusing, then add a property named pressure to it. And that all may even work fine until you start loosing control or re-visit the same code a couple of weeks later.

再补充一点困惑:在同一个类中,你可以有一个实例变量压强,它会隐藏一个全局变量压强,如果有的话。命名为pressure的方法可能具有隐藏实例变量的参数压力。然后可以在该方法的不同作用域内声明多个变量压力。它们都将各自命名的副本隐藏在封闭范围内。如果你真的想让它混乱,那就添加一个属性,叫做压力。这一切甚至都可以正常工作,直到你开始失去控制或者几周后重新访问相同的代码。

Edit: the method pressure would clearly conflict with the getter of the property pressure. Meaning, it would work but the compiler would take the method as the getter given that it returns an object of the appropritate type. Or so.

编辑:方法压力将明显与属性压力的getter发生冲突。也就是说,它可以工作,但是编译器会把这个方法作为getter,因为它返回的对象是专用类型的对象。左右。