当前位置:网站首页>[@synthesize in Objective-C]
[@synthesize in Objective-C]
2022-08-01 21:34:00 【morning breeze】
Foreword
@property can only generate declarations of getter and setter methods, what about the implementation?
1. What is @synthesize?
For example, there is a class
#import
@interface Person : NSObject
{
NSString *_name;
int _age;
float _weight;
}
@property NSString *name;
@property int age;
@property float weight;
@end
@implementation Person
@end
So, can the implementation of getter and setter methods be automatically generated?
1) The role of @synthesize: automatically generate the implementation of getter and setter methods.
2) Syntax: @synthesize @property name;
For example:
@interface Person: NSObject
{
int _age;
}
@property int age;
@end
----------------------
@implementation Person
@synthesize age;
@end
2. What @synthesize does
1. Generate a true private attribute
1) What is a true private property, that is, declared in @implementation,
2) The type of this property is consistent with the @property type corresponding to @synthesize;
3) The name of the property is the same as @The @property name corresponding to synthesize is the same;
For example, this code:
@implementation Person
@synthesize age;
@end
translates to
@implementation Person
{
int age;
}
- (void)setAge:(int)age;
{
self->age = age;
} - (int)age
{
return age;
}
@end
4) Automatically generate the implementation of the setter method
Implementation method: directly pass the value of the parameter age to it to generateThe private property age;
5) Automatically generate the implementation of the getter method
Implementation method: return the value of the private property that it automatically generates
2. How to make @synthesize not automatically generate private attributes, just use our own defined underlined attributes, that's it.
Syntax: @synthesize @property name = existing property name;
Example: @synthesize age = _age;
So, what does this sentence mean?
1) No private properties are generated anymore;
2) The implementation of getter and setter methods is directly generated;
3) How is the setter implemented?Assign the value of the parameter to an existing underscore attribute
For example:
- (void)setAge:(int)age
{
_age = age;
}
4) How is the getter implemented?Returns the value of the existing underscore property directly.
- (int)age
{
return _age;
}
Summary
When using @synthesize, there are a few points to note:
1) If you directly write a @synthesize
For example: @synthesize name;
Then, it automatically generates a private property, and the operation isAutomatically generated private attributes
2) If you specify the attributes of the operation
For example: @synthesize name = _name;
Then, it will not automatically generate private attributes, and the operation is the underlined one we specifiedAttribute;
3) In the implementation of the generated setter method, there is no logic verification, and it is directly assigned.If you want to implement logic verification, you need to write the implementation of the setter method yourself.For example:
- (void)setAge:(int)age
{
if(age >= 0 && age <= 120)
{
_age = age;
}
else
_age = 18;
}
4) Batch declaration
For example:
@property float height , weight;
If the types of multiple @property are consistent, they can be declared in batches;
@synthesize name, age, weight, height;
@synthesize name = _name,age=_age,weight=_weight,height= _height;
The types of @synthesize are inconsistent and can also be declared in batches.
边栏推荐
- Review Set/Map basics with these two hooks
- FusionGAN:A generative adversarial network for infrared and visible image fusion article study notes
- 附录A printf、varargs与stdarg A.2 使用varargs.h来实现可变参数列表
- C Pitfalls and pitfalls Appendix B Interview with Koenig and Moo
- C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.4 K&R C
- 虚拟内存与物理内存之间的关系
- Appendix A printf, varargs and stdarg A.3 stdarg.h ANSI version of varargs.h
- JS hoisting: how to break the chain of Promise calls
- 磷酸化甘露糖苷修饰白蛋白纳米粒/卵白蛋白-葡聚糖纳米凝胶的
- C Pitfalls and Defects Chapter 7 Portability Defects 7.6 Memory Location 0
猜你喜欢
随机推荐
pytest:开始使用
淘宝获取收货地址列表的 API
磷酸化甘露糖苷修饰白蛋白纳米粒/卵白蛋白-葡聚糖纳米凝胶的
在Cesium中实现与CAD的DWG图叠加显示分析
C陷阱与缺陷 第7章 可移植性缺陷 7.7 除法运算时发生的截断
软考 ----- UML设计与分析(上)
ISC2022 HackingClub白帽峰会倒计时1天!最全议程正式公布!元宇宙集结,精彩绝伦!
render-props和高阶组件
附录A printf、varargs与stdarg A.3 stdarg.h ANSI版的varargs.h
【Unity实战100例】文件压缩Zip和ZIP文件的解压
HCIP---多生成树协议相关知识点
MySQL相关知识
Pytest: begin to use
Anacoda的用途
Jmeter combat | Repeated and concurrently grabbing red envelopes with the same user
WEB 渗透之端口协议
C专家编程 第1章 C:穿越时空的迷雾 1.2 C语言的早期体验
回收租凭系统100%开源无加密 商城+回收+租赁
Appendix A printf, varargs and stdarg A.3 stdarg.h ANSI version of varargs.h
树莓派的信息显示小屏幕,显示时间、IP地址、CPU信息、内存信息(c语言),四线的i2c通信,0.96寸oled屏幕









