在Objective-C中消除歧义名称与实例变量名称的良好做法 [英] Good practice for disambiguating argument names versus instance variable names in Objective-C

查看:137
本文介绍了在Objective-C中消除歧义名称与实例变量名称的良好做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Objective-C中遇到一个相当普遍的情况,我将一个变量传递给一个init方法,然后将它赋给一个同名的实例变量。但我没有找到一个方法来范围的变量来澄清哪个是来自消息参数的值和哪个是实例变量。



说我有一些类,如如下:

  @interface MyObject 
{
NSString * value;
}
- (id)initWithValue:(NSString *)value;
@end



在我的实现中,我想要我的init方法看起来像这样: / p>

   - (id)initWithValue:(NSString *)value 
{
self = [super init];
if(self){
self.value = value; //这将不适用于实例变量
}
}

的三个解决方案:


  1. 创建一个属性,允许调用 self.value

  2. 重命名我的实例变量,例如 _value

  3. 重命名我的init参数变量,例如 initValue argValue

我对这些解决方案不满意。添加属性或者使该属性在接口上公开可用,或者如果我使用扩展,则从继承器隐藏它。我也不喜欢为变量使用不同的名称或使用下划线,这可能来自其他语言如Java和C#的开发。



有办法从消息参数中消除实例变量?如果没有,是否有一个编程指南在Cocoa如何解决这个问题?



在考虑如何使用后,在C中这样做,我想出了 self> value 的解决方案。这个工作,但它产生一个编译器警告,'本地声明'值'隐藏实例变量。因此,这不是一个令人满意的解决方案,因为我有一个零警告目标。

解决方案

对于setters(和扩展初始化器),我相信约定是前缀参数名称 new

   - (void)setCrunk:(Crunk *)newCrunk; 
- (id)initWithCrunk:(Crunk *)newCrunk;

一般来说,我认为最常见的形式是调用参数 theCrunk ,但苹果似乎推荐 aCrunk


I run into a fairly common scenario in Objective-C where I pass in a variable to an init method and then want to assign it to an instance variable of the same name. However I have not found a way to scope the variables to clarify which is the value from the message argument and which is the instance variable.

Say I have some class such as the following:

@interface MyObject
{
    NSString *value;
}
- (id)initWithValue:(NSString *)value;
@end

In my implementation I want my init method to look something like this:

- (id)initWithValue:(NSString *)value
{
    self = [super init];
    if(self) {
        self.value = value;  // This will not work with instance variables
    }
}

I know of three solutions:

  1. Create a property, which allows calling self.value
  2. Rename my instance variable, such as _value
  3. Rename my init argument variable, such as initValue or argValue

I am not pleased with any of these solutions. Adding a property either makes the property publicly available on the interface or, if I use an extension, hides it from inheritors. I also do not like having different names for the variables or using an underscore, which perhaps comes from developing in other languages such as Java and C#.

Is there a way to disambiguate instance variables from message arguments? If not, is there a coding guideline in Cocoa for how to solve this problem? I like following style guidelines when appropriate.

Update

After thinking about how to do this in C, I came up with the solution of self->value. This works, but it produces a compiler warning that the Local declaration of 'value' hides instance variable. So this is not a satisfactory solution either since I have a zero-warning goal.

解决方案

For setters (and by extension initializers), I believe the convention is to prefix the parameter name with new:

- (void)setCrunk:(Crunk *)newCrunk;
- (id)initWithCrunk:(Crunk *)newCrunk;

In general, I think the most common form I've seen is to call the parameter theCrunk, but Apple seems to recommend aCrunk.

这篇关于在Objective-C中消除歧义名称与实例变量名称的良好做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆