在Objective C中的实例变量/方法参数命名 [英] instance variable/ method argument naming in Objective C

查看:153
本文介绍了在Objective C中的实例变量/方法参数命名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,为了命名实例变量和方法参数,特别是当方法参数用于设置ivars(实例变量)时,有什么约定?



在C ++中用于对ivars使用 m _ 前缀很多。在C#中,我遵循纯粹通过使用这样来消除歧义的约定。 for ivars。我已经采用了C ++中的等价( this - > )。



在目标C中,我尝试了几件事,但没有一件事情真的很好。



真的很好我会辞职的事实,我将不得不妥协(但请不要让我使用 前缀的args!),所以我'有兴趣听取多数人的意见 - 特别是那些已经使用ObjC一段时间的人。



我在发布之前做了一些尽职调查和一些好资源我在哪里找到:





他们给了我一些想法,但我仍然渴望听到别人在做什么。




只是为了澄清:区分ivars和方法args,我正在寻找 - 无论是通过前缀或一些其他技术。




感谢所有的响应和讨论点。我不会关闭这个,但只是说,正如我在我的意见中指出接受的答案,我已经习惯了前缀初始args与 (和setter args with new ,这是我反正)。

解决方案

正如你所说的那样,注意到,Cocoa风格是使用方法参数名称,如 theValue 如果参数名称将与实例变量冲突。但是,在Objective-C 2.0风格的代码中不应该出现很多次。假设你不应该(通常)直接访问实例变量。这主要是因为这样做规避了可可的关键价值观察机制。相反,期望是你将通过getter / setter方法访问和改变属性。在Objective-C 2.0中,很容易声明这些属性,并自动地 @synthesize getters / setters,所以没有太多理由不使用它们。事实上,在64位系统上,运行时将为您自动创建实例变量,避免需要声明它们,减少使用它们的冲动。



只有直接访问实例变量的时间是 -init -dealloc 方法:

  @interface MyObject:NSObject 
{
id ivar;
}

@property(retain,readwrite)id ivar; //或任何保留/复制/分配和读/读写有意义
@end

@implementation MyObject
@synthesize ivar;

- (id)initWithIvar:(id)theIvar {
if(self = [super init]){
ivar = theIvar;
}

return self;
}

- (void)dealloc {
[ivar release];
}

在这些情况下应该直接使用ivar的原因是beacuse getter / setter可能具有依赖于完全初始化的实例的副作用,从而使它们在 -init -dealloc 中是危险的对象的状态完全初始化。在所有其他情况下,您应该使用 self.ivar (或 [self ivar] -initWithXX

$ c>不应该有命名冲突。如果他们这样做,他们不应该重构不具有该参数或者类不具有实例变量?



这只剩下 -initWithXX 方法,其中您经常会发现参数和ivars之间的冲突。对于这种情况,你可以使用你提到的任何方法,如果你真的不能忍受可可风格。前缀与 _ 工程和相对常见(我相信 @synthesize 'setter和getter将自动做正确在这种情况下,但您可能必须显式设置 _ivar 作为背景)。


What conventions are people here following for naming of instance variables and method arguments - particularly when method arguments are used to set ivars (instance variables)?

In C++ I used to use the m_ prefix for ivars a lot. In C# I followed the convention of disambiguating purely by use of this. for ivars. I've since adopted the equivalent in C++ too (this->).

In Objective C I've tried a few things but none have really seemed satisfactory.

Unless someone suggests something really nice I am resigned to the fact that I'll have to compromise (but please, don't make me use the the prefix for args!), so I'm interested to hear what the majority say - especially from those who have been using ObjC for a while.

I did some due diligence before posting this and a couple of good resources I found where:

They give me some ideas, but I'm still keen to hear what others are doing.

[edit] Just to clarify: It's specifically how you distinguish ivars from method args that I'm looking for - whether that's through prefixes or some other technique.

[edit 2] Thanks for all the responses and discussion points. I'm not closing this, but will just say that, as I indicated in my comments to the accepted answer, I've gone with the convention of prefixing init args with the (and setter args with new, which I was doing anyway). This seems to be the best balance of forces - even if I'm not keen on the aesthetic myself.

解决方案

As you've noted, the Cocoa style is to use method argument names like theValue if the argument name will conflict with an instance variable. There shouldn't be many times that this comes up in Objective-C 2.0 style code, however. The assumption is that you shouldn't (usually) be accessing instance variables directly. Mostly this is because doing so circumvents the Key-Value Observing machinery in Cocoa. Rather, the expectation is that you'll access and mutate properties via getter/setter methods. In Objective-C 2.0, it's easy to declare these properties and automatically @synthesize the getters/setters, so there's not much excuse for not using them. In fact, on 64-bit systems, the runtime will automatically create the instance variables for you, obviating the need to declare them and reducing the urge to use them.

The only time you should be accessing instance variables directly is in -init and -dealloc methods:

@interface MyObject : NSObject 
{
  id ivar;
}

@property (retain,readwrite) id ivar; //or whatever retain/copy/assign and read/readwrite makes sense
@end

@implementation MyObject
@synthesize ivar;

- (id)initWithIvar:(id)theIvar {
  if(self = [super init]) {
    ivar = theIvar;
  }

  return self;
}

- (void)dealloc {
  [ivar release];
}

The reason the ivar should be used directly in these cases is beacuse the getter/setter may have side effects that depend on a fully initialized instance, thus making them dangerous in -init and -dealloc where the state of the object is fully initialized. In all other cases, you should be using self.ivar (or [self ivar] and [self setIvar:newValue]).

It would seem that methods other than -initWithXX shouldn't have the naming conflict. If they do, shouldn't they be refactored to not have that parameter or for the Class to not have the instance variable?

This leaves just the -initWithXX methods where you would often find a conflict between arguments and ivars. For this case, you can use any of the approaches you mention if you really can't stand the Cocoa style. Prefixing with _ works and is relatively common (I believe the @synthesize'd setters and getters will automatically do the right thing in this case, but you may have to explicitly set the _ivar as the backing).

这篇关于在Objective C中的实例变量/方法参数命名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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