在目标c中使用ivars vs属性的原因 [英] Reason to use ivars vs properties in objective c

查看:81
本文介绍了在目标c中使用ivars vs属性的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直无法找到有关此主题的任何信息,而我所知道的大部分信息都是完全出于偶然(以及数小时的尝试来弄清楚为什么我的代码不起作用的原因).在学习Objective-C时,我发现大多数教程都使用相同的名称制作变量和属性.我不理解其重要性,因为似乎该属性完成了所有工作,而该变量只是位于那里.例如:

I have been unable to find any information on this topic and most of what I know about it has come by complete accident (and a few hours of trying to figure out why my code wasn't working). While learning objective-c most tutorials I have found make variables and properties with the same name. I don't understand the significance because it seems that the property does all the work and the variable just kind of sits there. For instance:

Test.h

@interface Test : NSObject {
    int _timesPlayed, _highscore;
}

@property int timesPlayed, highscore;

// Methods and stuff

@end

Test.m

@implementation Test

  @synthesize timesPlayed = _timesPlayed;
  @synthesize highscore   = _highscore;

  // methods and stuff

@end

我所知道的

1)好吧,今天我发现(经过数小时的混乱),无论您对属性highscore = 5091231进行多少更改,当您尝试调用[test highscore]时,它都不会改变任何内容,因为它仍然会返回_highscore的值(我认为)是在test.h中设置的ivar.因此,test.m中变量的所有更改都需要更改_highscore而不是highscore. (如果我在这里错了,请纠正我)

1) Okay so today I found out (after hours of confusion) that no matter how much changing you do to the properties highscore = 5091231 it won't change anything when you try to call [test highscore] as it will still be returning the value of _highscore which (I think) is the ivar that was set in test.h. So all changing of variables in test.m needs to be changing _highscore and not highscore. (Correct me if I'm wrong here please)

2)如果我正确理解(可能不是),则test.h中设置的ivars代表实际的内存,其中@properties只是访问该内存的方式.因此,在实现之外,如果不通过该属性,我将无法访问_highscore.

2) If I understand it correctly (I probably don't) the ivars set in test.h represent the actual memory where as the @properties are just ways to access that memory. So outside of the implementation I can't access _highscore without going through the property.

我不了解的内容

基本上,我对这种情况不了解的是我是否完全需要使用ivars,或者是否可以只使用@property和@synthesize.似乎ivars只是多余的代码,除了使我困惑之外,它实际上并没有做任何事情.我见过的一些最近的tut似乎并没有使用ivars,但是有些则使用了.那么,这仅仅是编码方面的优先事项,还是实际上很重要?我曾尝试过搜索Apple的文档,但我在那里迷失了,似乎再也找不到我想要的东西.任何指导将不胜感激.

Basically what I don't get about this situation is whether or not I need to use the ivars at all or if I can just use @property and @synthesize. It seems like the ivars are just extra code that don't really do anything but confuse me. Some of the most recent tuts I've seen don't seem to use ivars but then some do. So is this just a coding preference thing or is it actually important? I have tried searching through Apple's Documentation but I get rather lost in there and never seem to find what I'm looking for. Any guidance will be greatly appreciated.

推荐答案

您可以将用于综合属性的语法视为@synthesize propertyName = variableName.

You can think of the syntax for synthesizing properties as @synthesize propertyName = variableName.

这意味着,如果您编写@synthesize highscore = _highscore;,将为您创建一个名称为_highscore的新ivar.因此,如果您愿意,可以直接转到_highscore变量来访问存储属性的变量.

This means that if you write @synthesize highscore = _highscore; a new ivar with the name _highscore will be created for you. So if you wanted to you could access the variable that the property is stored in directly by going to the _highscore variable.

在我不记得综合语句未创建ivar的某个版本的编译器之前.相反,它只说了应该使用什么变量,因此您必须声明变量和属性.如果您使用下划线前缀进行合成,则您的变量需要具有相同的前缀.现在您不再需要自己创建该变量,而是将创建一个在综合语句中指定的带有variableName的变量(如果您尚未自己声明它,在这种情况下,它将用作属性的后备变量).

Prior to some version of the compiler that I don't remember the synthesis statement didn't create the ivar. Instead it only said what variable it should use so you had to declare both the variable and the property. If you synthesized with a underscore prefix then your variable needed to have the same prefix. Now you don't have to create the variable yourself anymore, instead a variable with the variableName that you specified in the synthesis statement will be created (if you didn't already declare it yourself in which case it is just used as the backing variable of the property).

在声明变量时,您正在显式创建一个名为highscore的ivar,然后在综合属性时隐式创建了另一个名为_highscore的ivar.这些不是相同的变量,因此更改其中一个不会改变其他变量.

You are explicitly creating one ivar called highscore when declaring the variable and then implicitly creating another ivar called _highscore when synthesizing the property. These are not the same variable so changing one of them changes nothing about the other.

这实际上是关于首选项的问题.

This is really a question about preference.

如果您不必到处写self.,则有些人会认为代码变得更加简洁.人们还说它更快,因为它不需要方法调用(尽管它可能永远不会对您的应用程序性能产生可衡量的影响).

Some people feel that the code becomes cleaner if you don't have to write self. all over the place. People also say that it is faster since it doesn't require a method call (though it is probably never ever going to have a measurable effect on your apps performance).

更改属性的值将调用所有必需的KVO方法,以便在值更改时可以通知其他类.默认情况下,对属性的访问也是原子的(不能从多个线程访问),因此可以更安全地从多个线程读写属性(这并不意味着该属性指向的对象是线程安全的)这是一个可变的数组,因此多个线程仍然可以破坏很多东西,这只会阻止两个线程将属性设置为其他东西.

Changing the value of the property will call all the necessary KVO methods so that other classes can get notified when the value changes. By default access to properties is also atomic (cannot be accessed from more then one thread) so the property is safer to read and write to from multiple thread (this doesn't mean that the object that the property points to is thread safe, if it's an mutable array then multiple thread can still break things really bad, it will only prevent two threads from setting the property to different things).

这篇关于在目标c中使用ivars vs属性的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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