问题描述
在目标C中您想使用实例变量vs属性的情况是什么?有人可以提供真实的例子吗?
What is a case in Objective C where you would want to use an instance variable vs property? Can someone provide a real-life example?
推荐答案
实例变量对于类是唯一的.默认情况下,只有类和子类可以访问它.因此,作为面向对象编程的基本原理,实例变量(ivars)是私有的,它们被类封装.
An instance variable is unique to a class. By default, only the class and subclasses can access it. Therefore, as a fundamental principal of object-oriented programming, instance variables (ivars) are private—they are encapsulated by the class.
相比之下,属性是一个公共值,它可能与实例变量相对应,也可能不与实例变量相对应.如果要公开一个ivar,则可能要设置一个对应的属性.但是同时,您希望保持私有的实例变量没有相应的属性,因此无法从类外部访问它们.您还可以具有与ivar不对应的计算所得属性.
By contrast, a property is a public value that may or may not correspond to an instance variable. If you want to make an ivar public, you'd probably make a corresponding property. But at the same time, instance variables that you wish to keep private do not have corresponding properties, and so they cannot be accessed from outside of the class. You can also have a calculated property that does not correspond to an ivar.
没有属性,ivars可以保持隐藏.实际上,除非在公共头文件中声明了ivar,否则甚至很难确定这样的ivar存在.
Without a property, ivars can be kept hidden. In fact, unless an ivar is declared in a public header it is difficult to even determine that such an ivar exists.
一个简单的类比就是一本收缩包装的书.一个属性可以是title
,author
或精装与软装. 错误"将是本书的实际内容.除非您拥有这本书,否则您将无权访问实际文本;除非您拥有课程,否则您将无权访问ivars.
A simple analogy would be a shrink-wrapped book. A property might be the title
, author
or hardcover vs. softcover. The "ivars" would be the actual contents of the book. You don't have access to the actual text until you own the book; you don't have access to the ivars unless you own the class.
更有趣的是,属性可以更好地集成到运行时中.现代的64位运行时将为访问器属性生成一个ivar,因此您甚至无需创建ivar.属性实际上是方法:
More interestingly, properties are better integrated into the runtime. Modern 64-bit runtimes will generate an ivar for accessor properties, so you don't even need to create the ivar. Properties are in fact methods:
// This is not syntactically correct but gets the meaning across
(self.variable) == ([self variable];)
(self.variable = 5;) == ([self setVariable:5];)
对于每个属性,有两种方法(除非将该属性声明为readonly
,在这种情况下只有一个):有一个 getter ,它返回与ivar相同的类型并且与ivar和 setter (未使用readonly
ivar声明)同名;它返回void,其名称只是 set 放在变量名之前.
For every property, there are two methods (unless the property is declared readonly
, in which case there is only one): there is the getter, which returns the same type as the ivar and is of the same name as the ivar, as well as the setter (which is not declared with a readonly
ivar); it returns void and its name is simply set prepended to the variable name.
由于它们是方法,因此可以对其进行动态调用.使用 NSSelectorFromString()
和各种 performSelector:
方法,您可以使用许多方法制作动态程序可能性.
Because they are methods, you can make dynamic calls on them. Using NSSelectorFromString()
and the various performSelector:
methods, you can make a very dynamic program with many possibilities.
最后,属性在Core Data中广泛使用,并且与键值编码. Core Data是一个高级框架,用于在SQLite数据库中存储数据的同时提供清晰的Obj-C前端; KVC用于整个Core Data,并且是一种动态访问属性的方式.在对对象进行编码/解码(例如从XIB读取时)时使用.
Finally, properties are used extensively in Core Data and with Key-Value Coding. Core Data is an advanced framework for storing data in a SQLite database while providing a clear Obj-C front-end; KVC is used throughout Core Data and is a dynamic way to access properties. It is used when encoding/decoding objects, such as when reading from XIBs.
这篇关于属性与实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!