@property和@synthesize有什么意义? [英] What is the point of @property and @synthesize?

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

问题描述

我无法弄明白,并且没有网站可以清楚地解释它...... @property 的目的究竟是什么? code> @synthesize ?

I haven't been able to figure it out, and there are no websites which explain it clearly enough... what exactly are the purposes of @property and @synthesize?

提前致谢!

推荐答案

Objective-C运行时编程指南:声明的属性

@property 声明要实现的公共属性的getter和setter方法。例如,这个属性声明:

@property declares the getter and the setter methods for the public property you want to implement. For example this property declaration:

@property float value;

相当于:

- (float)value;
- (void)setValue:(float)newValue;

@synthesize 为这两个提供默认实现访问者。

@synthesize provides default implementation for these two accessors.

更新:以上解释了这两者的作用。它没有解释他们的目的是什么。 : - )

Update: The above explains what these two do. It does not explain what their purpose is. :-)


  • @property 将一个成员添加到充当公共接口的成员类客户端的数据变量,但使用方法读取和写入。这使您可以更好地控制客户端和代码之间交换的数据,例如,您可以对代码赋予的值进行扩展验证。

  • @synthesize 允许您不显式编写客户端将调用的代码,并将该属性视为数据变量你自己。

  • @property adds a member to the public interface that acts as a data variable to your class clients, but is read and written using methods. This gives you better control over the data that is exchanged between the client and your code, for example you can do extended validation on the values your code is given.
  • @synthesize allows you to not explicitly write the code that will be called by the client and actually treat the property as a data variable yourself.

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

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