在Objective-C 2.0中使用下划线并使用@synthetize重命名的实例变量通过Xcode 4的“Analyze”工具导致优化警告 [英] Instance variables with underscore in Objective-C 2.0 and renaming with @synthetize leads to optimization warnings by the 'Analyze' tool of Xcode 4

查看:154
本文介绍了在Objective-C 2.0中使用下划线并使用@synthetize重命名的实例变量通过Xcode 4的“Analyze”工具导致优化警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

我正在使用相同的约定实例变量和属性命名,如下面的sebnow所示回答:

I'm using the same convention for instance variable and properties naming as shown by sebnow in his following answer:

目标C中的实例变量/方法参数命名

我复制粘贴他的示例代码:

I copy paste his example code here:

@interface Foo : NSObject {
    id _bar;
}
@property (nonatomic, retain) id bar;

- (id) initWithBar:(id)aBar;

@end

@implementation Foo
@synthesize bar = _bar;

- (id) initWithBar:(id)aBar {
    self = [super init];
    if(self != nil) {
        _bar = aBar;
    }
    return self;
}

@end

在某些方法的实施中对于Foo类,我使用例如:

In the implementation of some methods of the Foo class, I use for example:

_bar = aBar

而不是使用:

bar = aBar

Xcode 4引入的'Analyze'工具给了我这个警告(我使用的是4.0.2版本):

The 'Analyse' tool introduced by Xcode 4 gives me this warning (I'm using version 4.0.2):

类'Foo'中的实例变量'bar'从未被@implementation中的方法使用(尽管它可能被类别方法使用)

也许我应该使用:

self.bar = aBar

但对于readonly属性,这是行不通的,除此之外,我不是确定在类中使用setter本身是不是一个好习惯。

But for the readonly properties, that can't work, and beside that, I'm not sure if using the setter in the class itself is a good practice or not.

我在Objective-C中并不新鲜,但我还处于初期阶段学习。也许我做错了什么,并且在某处做了错误的编码练习。

I'm not fresh in Objective-C, but I'm still in the beginning of learning. Perhaps I'm doing something wrong, and have a bad coding practice somewhere.

如果你能帮助我,请提前感谢你;)

Thanks you in advance if you can help me ;)

推荐答案

@synthesize 行会影响属性bar的setter和getter的运行方式。该行:

The @synthesize line affects how the setter and getter for the property 'bar' operate. The line:

@synthesize bar = _bar;

有效地说按标准放入标准吸气剂(和相关设定器,如果相关)方式我已将其声明为 @property ,但使用实例变量_bar作为存储。

Effectively says "put in the standard getter (and setter, if relevant) for bar, as per the way I've declared it as a @property, but use the instance variable _bar for the storage".

当你使用self.bar作为左值时,你实际上正在对 [self setBar:] 进行方法调用,当你将它用作右值时,你实际上正在制作一个拨打 [自助栏] 。它看起来像一个普通的C风格结构成员访问,但在内部它是一个方法调用。

When you use self.bar as an lvalue you're actually making a method call to [self setBar:] and when you use it as an rvalue you're actually making a call to [self bar]. It looks like a normal C-style struct member access but internally it's a method call.

所以, @synthesize 创建一个合适的getter和setter用于 self.bar ,但不会更改实例变量的名称。因此,当你直接从类本身访问这个东西时,你应该正确使用 _bar (虽然有些人现在从风格的角度来看这个)并且 self.bar 否则,没有收到任何分析器警告。

So, the @synthesize creates a suitable getter and setter to use for self.bar, but doesn't change the name of the instance variable. You should therefore be right to use _bar when accessing the thing directly from within the class itself (though some people now frown upon that from a style point of view) and self.bar otherwise, without receiving any analyser warnings.

最后得到一个名为<$ c $的实例变量c> bar ,假设您没有在界面中声明一个,最可能的错误是您执行 @synthesize 。在现代运行时,您可以为您未在界面中实际声明的变量提供 @ property / @ synthesize 对,并且该变量将神奇地添加到您的界面中。如果你犯了一个不幸的拼写错误,那么你可以意外地做到这一点。

For you to end up with an instance variable called bar, assuming you didn't declare one inside your interface, the most likely mistake is an error in the way you've performed your @synthesize. In the modern runtime you can supply a @property/@synthesize pair for a variable you haven't actually declared in your interface and the variable will be magically added to your interface. So you can do that by accident if you make an unfortunate typo.

如果可能的话,你能发布你的实际代码吗?

If possible, could you post your actual code?

这篇关于在Objective-C 2.0中使用下划线并使用@synthetize重命名的实例变量通过Xcode 4的“Analyze”工具导致优化警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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