What is the point of @property and @synthesize?(@property 和 @synthesize 的意义何在?)
问题描述
我没弄明白,也没有网站能解释得够清楚……@property和@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允许您不显式编写将由客户端调用的代码,而实际上您自己将属性视为数据变量.
@propertyadds 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.@synthesizeallows 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 的意义何在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:@property 和 @synthesize 的意义何在?
基础教程推荐
- iOS - UINavigationController 添加多个正确的项目? 2022-01-01
- Android Volley - 如何动画图像加载? 2022-01-01
- SwiftUI-ScrollViewReader的ScrollTo不滚动 2022-01-01
- navigationItem.backBarButtonItem 不工作?为什么上一个菜单仍然显示为按钮? 2022-01-01
- Play 商店的设备兼容性问题 2022-01-01
- 如何比较两个 NSDate:哪个是最近的? 2022-01-01
- UIImage 在开始时不适合 UIScrollView 2022-01-01
- Xcode UIView.init(frame:) 只能在主线程中使用 2022-01-01
- 为什么姜饼模拟器方向卡在应用程序中? 2022-01-01
- 如何将图像从一项活动发送到另一项活动? 2022-01-01
