什么时候使用@property? [英] When to use @property?

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

问题描述


可能重复:

我为什么要使用@properties?

何时使用目标C中的属性?

我有我已经在objective-c中编程了一年多一点,而且我总觉得使用@property和@synthesize这是一个惯例。但他们真正服务的目的是什么?他们是否只是在课堂之间进行沟通?因此,例如,如果我仅在声明它的类的范围内使用NSMutableDictionary,则遗漏是O.K. ?

I have been programming in objective-c for a little over a year now, and I always felt like it was a convention to use @property and @synthesize. But what purpose do they really serve ? Are they solely there to be communicated between classes ? So, for instance, if I use an NSMutableDictionary only in the scope of the class where it was declared, an omission is O.K. ?

另一个问题:

如果我设置了NSMutableDictionary的属性,它会被保留,对吗?所以,在我的课上我不必调用alloc()和init(),是吗?
使用属性的规则是什么?

If I set the property of an NSMutableDictionary, it is retained, right ? So, in my class I don't have to call alloc() and init(), do I ? What are the rules to use properties ?

推荐答案


但他们真正服务的目的是什么?

But what purpose do they really serve?

对iVars的访问控制以及表示和基础数据之间的抽象。

Access control to iVars and abstraction between representation and underlying data.


他们是否单独在类之间进行通信?

Are they solely there to be communicated between classes?

不,它们适用于您希望控制对iVars的访问而不是直接访问它们或何时您可以在将来更改基础数据结构但希望保留当前表示。

No, they are for when you want to control access to iVars instead of accessing them directly or when you could in the future change underlying data structures but wish to keep the current representation.


因此,例如,如果我仅使用NSMutableDictionary在宣布它的类的范围内,遗漏是否可以?

So, for instance, if I use an NSMutableDictionary only in the scope of the class where it was declared, an omission is O.K.?

这取决于。您想要控制访问iVar吗?您的代码是否可以更改,以便获取字典而不是直接的iVar。通常,答案是肯定的。

It depends. Do you want to have controlled access to the iVar? Would it be possible for your code to change so the dictionary is fetched and not a direct iVar. Usually, the answer is yes.


如果我设置NSMutableDictionary的属性,它会被保留,对吗?

If I set the property of an NSMutableDictionary, it is retained, right?

取决于你如何申报财产。

Depends on how you declare the property.


所以,在我的班级我没有必要调用alloc()和init(),是吗?

So, in my class I don't have to call alloc() and init(), do I?

你这里有粗心的措辞。我想你在问你是否还需要构建一个属性的实例。是的,您需要以某种方式构建属性的实例。有很多方法可以做到这一点。

You have sloppy wording here. I think you are asking if you still need to construct an instance of a property. Yes, you will need to construct an instance of a property in some way. There are lots of ways of doing this.

注意:谈论方法的惯例是使用它们的签名。而不是alloc(),你会使用-alloc。

NOTE: the convention for talking about methods is use their signature. Instead of alloc(), you would use -alloc.


使用属性的规则是什么?

What are the rules to use properties?

您需要阅读 doc for。

这篇关于什么时候使用@property?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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