简体   繁体   English

声明一个iVar,属性

[英]Declaring an iVar, property

I have just inherited an iOS project and just have a query about the way the ivars and properties are assigned. 我刚刚继承了一个iOS项目,并且只查询了有关ivars和属性的分配方式。

In this project ivars are named similar to the property but begin with an underscore. 在该项目中,ivars的名称类似于该属性,但以下划线开头。 Then, when the property is being synthesized it is done like this 然后,在合成属性时就这样完成

@synthesize property = _ivar;

I find this a little odd. 我觉得这有点奇怪。 I have just worked on one iOS project before but when I was doing both the ivar and property had the same name. 我之前只从事过一个iOS项目,但是当我做ivar和property时,它们具有相同的名称。 I would then simply synthesize the property and everything was hunky dory. 然后,我将简单地合成该属性,并且一切都是笨拙的。

Why did this guy do things differently? 为什么这个人做事与众不同? What am I missing? 我想念什么?

Somewhere along the line it became conventional to use the _var form as protection against accidentally referencing the variable when the developer should have been referencing self.property . 在开发人员本应引用self.property ,使用_var形式作为防止意外引用变量的保护措施已成为self.property It is now established to the point that, with the latest tools, you can drop the @synthesize lines entirely and @synthesize x = _x; 现在已经确定,使用最新的工具,您可以完全删除@synthesize行,而@synthesize x = _x; is simulated automatically. 是自动模拟的。

You not missing anything, it a choice of style. 您不会错过任何东西,它是款式的选择。 When using the _ivar way you can't easily make a mistake with the ivar or the property. 当使用_ivar方法时,您不容易对ivar或属性犯错误。 Also as stated by Mike Weller this is what Apple recommends. 就像Mike Weller所说的那样,这也是Apple建议的。

If also help if you are using delegate where they pass the send with the same name as your ivar. 如果在使用委托的情况下也有帮助,则委托将传递与ivar相同名称的发送传递给委托。 For example: 例如:

If you declare a property tableView and do the same for the ivar then the compiler will complain about redeclaring the variable tableView in a method like: 如果您声明一个属性tableView并对ivar做同样的事情,那么编译器将抱怨用以下方法重新声明变量tableView

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   // Dot some thing with the table object
}

Because both the class scope and the method scope hava the variable name tableView and you can't really tell which one is to be used. 因为类作用域和方法作用域都具有变量名tableView ,所以您无法真正确定要使用哪个变量。

In the latest versions of Xcode you do not even have to declare the ivar's any more, if you header file looks like this: 在最新版本的Xcode中,如果头文件看起来像这样,您甚至都不需要声明ivar了:

@interface MyTableViewController : UIViewController <UITableViewDelegate> 
    @property (nonatomic, weak) IBOutlet UITableView *tableView;
@end

Then you don't even have to @synthesize the property any more, this is done for you automatically and a ivar with the name _tableView is also create. 然后,您甚至不必再@synthesize属性,它会自动为您完成,并且还会创建一个名为_tableView的ivar。 But accessing the variable via the property with the self.tableView syntax is preferred. 但是首选通过具有self.tableView语法的属性访问变量。

There is an exception on this, if you where to reintroduce a property with which already exists in the super calls you must call @synthesize . 有一个例外,如果您要在超级调用中重新引入已经存在的属性,则必须调用@synthesize It will again create the '_ivar' for you. 它将再次为您创建“ _ivar”。

An other exception is that if you are using primitive types the compiler will not create the _ivar for you. 另一个例外是,如果您使用基本类型,则编译器将不会为您创建 _ivar You don't need to create them both in the header you can just do @synthesize myInt = _myInt and the ivar is created for you. 您无需在标头中都创建它们,只需执行 @synthesize myInt = _myInt创建ivar。

this practice forces you to call and set your iVars using property. 这种做法会迫使您调用并使用属性设置iVar。 Incase where you want to use iVar,u call it by _iVar, otherwise there will be no chance of calling iVar directly without using property by mistake. 如果要使用iVar,请通过_iVar调用它,否则将无法直接调用iVar,而不会错误使用属性。 It will show error and you will get reminded that oh i need to put self. 它会显示错误,并且您会被提醒,哦,我需要自我介绍。

In the latest version of XCode you need not to synthesize, it does for you. 在最新版本的XCode中,您不需要进行合成,而是为您完成。 But again with an underscore. 但是再次强调。

However you can override it by declaring it @synthesize property=property; 但是,您可以通过声明@synthesize property=property;来覆盖它@synthesize property=property; or @synthesize property; @synthesize property; and even you will be aware that you can give any other name @synthesize property=myproperty; 甚至您会意识到您可以给任何其他名称@synthesize property=myproperty;

However initially it was just a matter of style, like using variable name and action names eg numberArray, nameTextField, cancelButton, but now it is seldom used and people are asked to name numbers, name, cancel respectively. 但是最初只是样式问题,例如使用变量名和操作名,例如numberArray,nameTextField,cancelButton,但现在很少使用了,人们被要求分别命名数字,名称和取消。

But I would suggest to follow apple guidelines to use _property. 但我建议遵循Apple准则使用_property。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM