ObjectiveC ivars或@property [英] ObjectiveC ivars or @property

查看:135
本文介绍了ObjectiveC ivars或@property的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iPhone上工作之后,经过很多头疼和内存问题后我才从其他示例中了解到,我们不需要为头文件中定义的每个实例变量创建@properties。实际上我发现ivars很容易在我在课堂上的任何地方使用后分配和释放它,对于@properties我必须使用autorealese或者我有严重的问题并且因为我分配了...

Working on iPhone, after a lot of headache and memory problems I just realized from other examples that we do not need to necessarly create @properties for each instance variable we define in header file. And actually I found out ivars easy to just allocate and release it after I use anywhere in the class, for @properties I have to use autorealese or I have serious problems and becareful how I allocate..

例如,对于下面的对象,许多示例中的@properties(retain / copy ..)未在标题中使用;

For instance for objects below, @properties(retain/copy..) is not used in headers in many examples;

{
NSURLConnection *connection;
NSMutableData *xmlData;
NsMutableString *string
} 

但对于某些字符串或对象类型@properties使用,我知道当我们设置@property时,cocoa会创建一些setter getter来处理对象的重新关系和保留。但似乎对于xmlData或连接实例变量,我们不需要这样做,他们就是这样做的。

But for some strings or object types @properties is used, I know that when we set @property cocoa creates some setters getters which are handling the relasing and retaining of the objects. But seems like as for xmlData or connection instance variables we do not need that and they do their job like this.

在决定时是否有一些参考指南我可以记住是否创建@ property或仅使用简单的ivars?

Are there some reference guidelines I can keep in mind on deciding whether or not to create @property's or just use simple ivars?

使用属性时我唯一的问题是因为我懒得定义它,但是当我在代码中仔细分配并初始化它们时,我必须使用autorelase并且不要觉得就像我有控制什么时候释放重置并再次分配它,它给了我一个额外的事情担心的时间和时间以及如何释放,重置它。我发现ivars我可以随时轻松地分配和释放一次而不用担心任何事情......或者我在这里缺少其他东西。

My only problem when using properties is not becuase I am lazy to define it, but when I carefully allocate and init them in code, I have to use autorelase and dont feel like I have the control when to release reset and allocate it again, and it gives me one more thing to worry about while and when and how should I release, reset it. I find ivars I can alloc and release anytime once anywhere easily without worrying about anything..or I am missing other things here.

Tnx

推荐答案

似乎仍然存在一些关于财产的错误观念。

There seem to still be some misconceptions flying around about properties.


我们不需要为头文件中定义的每个实例变量创建@properties

that we do not need to necessarly create @properties for each instance variable we define in header file

正确。您可以直接在实现文件中使用私有实例变量。但是,由于合成属性带有可用内存管理,因此您也可以利用它。我的经验法则是直到我第一次写自己的时候直接使用ivar:

Correct. You can use private instance variables directly in your implementation file. However, since synthesized properties come with free memory management, you might as well take advantage. My rule of thumb is to use the ivar directly until the first time I find myself writing:

[ivar release];
ivar = [newIvar retain];

正如Sam所说,如果 iVar ==已经存在潜在的错误newIVar 。这是我从使用ivars直接切换到创建属性的关键点。但是,我将新属性的声明放在类扩展中在实施文件中。这意味着该属性正式不属于公共接口(如果意外使用,将导致编译器警告)。

As Sam says, there is already a potential bug there if iVar == newIVar. This is the point at which I switch from using ivars directly to creating a property. However, I put the declaration of the new property in a class extension in the implementation file. This means that the property is officially not part of the public interface (and will cause compiler warnings if used accidentally).


当我们设置@时property cocoa创建了一些setter getter,用于处理对象的重新发送和保留。

when we set @property cocoa creates some setters getters which are handling the relasing and retaining of the objects.

实际上,没有。 @property只声明了一个属性。为了自动生成getter和setter,你需要@synthesize它。您也可以编写自己的getter和setter,甚至不需要引用真正的ivar。

Actually, no. The @property just declares a property. In order to automatically generate the getter and setter, you need to @synthesize it. You could, alternatively write your own getters and setter which do not even have to reference a real ivar.

从技术上讲,您不应该在init或dealloc方法中使用该属性因为子类可能已经覆盖它们或者(在dealloc中)你可以设置KVO通知。

Technically, you should not use the property in the init or dealloc methods because a subclass might have overridden them or (in dealloc) you might set off a KVO notification.

来自Sam的回答和评论

From Sam's answer and comments


如果你想要一个属性,你可以在实现文件的顶部使用私有接口

If you want a property regardless, you could use a private interface at the top of the implementation file

正如我上面所说的那样,私有类别已经被类扩展废弃了(这已经足够相同了,但是允许你将方法的实现放在主类实现)。

As I say above, private categories have sort of been obsoleted by class extensions (which is near enough the same thing but allows you to put the implementation of the methods in the main class implementation).


如果你想要使用点符号简写的好处

If you want the benefits of using dot notation shorthand

我们中的一些人会认为点符号没有任何好处。这是结构成员语法的无偿和不必要的污染。但是,点符号与 @property 声明无关。您可以对任何访问者使用点表示法,无论它们如何声明,只要它们符合模式 -foo -setFoo:

Some of us would argue that there are no benefits to dot notation. It's a gratuitous and needless pollution of the struct member syntax. However, dot notation has no relation to @property declarations. You can use dot notation for any accessors no matter how they were declared, provided they adhere to the pattern -foo and and -setFoo:

这篇关于ObjectiveC ivars或@property的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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