参数隐藏Objective-C中的实例变量

时间:2022-09-07 19:38:28

Is there a way to give a parameter to a method the same name as an instance variable in Objective-C without hiding that variable?

有没有办法在不隐藏该变量的情况下为与Objective-C中的实例变量同名的方法提供参数?

For instance,

- (void)doSomething:(id)object
{
    self.object = object;
}

The code above gives the warning "local declaration of 'object' hides instance variable."

上面的代码给出了警告“对象'的本地声明隐藏实例变量。”

The obvious solution is to name the parameter arguments differently, but I find it annoying having to choose a name like "anObject" instead of "object".

显而易见的解决方案是以不同的方式命名参数参数,但我发现选择像“anObject”而不是“object”这样的名称很烦人。

2 个解决方案

#1


8  

You can use dot notation to access properties (as you do in the example), but instance variables have only one access path, so the only solution if you want to access both an instance variable and a local variable is to give them different names.

您可以使用点表示法来访问属性(如示例中所示),但实例变量只有一个访问路径,因此,如果要同时访问实例变量和局部变量,唯一的解决方案是为它们指定不同的名称。

Formally speaking, this is related to the restrictions on alpha conversion in lambda calculus, specifically that a bound variable should remain bound and a free variable remain free.

从形式上讲,这与lambda演算中alpha转换的限制有关,特别是绑定变量应保持绑定且*变量保持空闲。

If you don't like the "an" prefix for locals, you can use the "_" prefix convention for instance variables, as they're also effectively protected variables.

如果您不喜欢本地的“an”前缀,则可以对实例变量使用“_”前缀约定,因为它们也是有效保护的变量。

@interface AClass {
    id _object;
}
@property (retain) id object;
@end

@implementation AClass
@synthesize object = _object;

- (void)doSomething:(id)object
{
    [_object befriend:object];
}
...

Of course, "_" reads as "my", so it may be just as distasteful as "a"/"an" before parameters. Renaming is the best solution as instance and local variables have different roles, and their names should reflect this.

当然,“_”读作“我的”,因此它可能与参数之前的“a”/“an”一样令人反感。重命名是最佳解决方案,因为实例和局部变量具有不同的角色,它们的名称应该反映这一点。

#2


13  

You might be able to do something like self->object = object, but the Objective-C convention (derived from Smalltalk) is to prefix parameters with "a" or "an" if the parameter has the same name as an instance variable.

您可能能够执行self-> object = object之类的操作,但Objective-C约定(源自Smalltalk)是使用“a”或“an”为参数添加前缀(如果参数与实例变量同名)。

#1


8  

You can use dot notation to access properties (as you do in the example), but instance variables have only one access path, so the only solution if you want to access both an instance variable and a local variable is to give them different names.

您可以使用点表示法来访问属性(如示例中所示),但实例变量只有一个访问路径,因此,如果要同时访问实例变量和局部变量,唯一的解决方案是为它们指定不同的名称。

Formally speaking, this is related to the restrictions on alpha conversion in lambda calculus, specifically that a bound variable should remain bound and a free variable remain free.

从形式上讲,这与lambda演算中alpha转换的限制有关,特别是绑定变量应保持绑定且*变量保持空闲。

If you don't like the "an" prefix for locals, you can use the "_" prefix convention for instance variables, as they're also effectively protected variables.

如果您不喜欢本地的“an”前缀,则可以对实例变量使用“_”前缀约定,因为它们也是有效保护的变量。

@interface AClass {
    id _object;
}
@property (retain) id object;
@end

@implementation AClass
@synthesize object = _object;

- (void)doSomething:(id)object
{
    [_object befriend:object];
}
...

Of course, "_" reads as "my", so it may be just as distasteful as "a"/"an" before parameters. Renaming is the best solution as instance and local variables have different roles, and their names should reflect this.

当然,“_”读作“我的”,因此它可能与参数之前的“a”/“an”一样令人反感。重命名是最佳解决方案,因为实例和局部变量具有不同的角色,它们的名称应该反映这一点。

#2


13  

You might be able to do something like self->object = object, but the Objective-C convention (derived from Smalltalk) is to prefix parameters with "a" or "an" if the parameter has the same name as an instance variable.

您可能能够执行self-> object = object之类的操作,但Objective-C约定(源自Smalltalk)是使用“a”或“an”为参数添加前缀(如果参数与实例变量同名)。