当前位置:网站首页>[@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.
边栏推荐
- C陷阱与缺陷 第7章 可移植性缺陷 7.10 首先释放,然后重新分配
- C陷阱与缺陷 第7章 可移植性缺陷 7.7 除法运算时发生的截断
- JS提升:手写发布订阅者模式(小白篇)
- Spark cluster construction
- Based on php Xiangxi tourism website management system acquisition (php graduation design)
- C陷阱与缺陷 第7章 可移植性缺陷 7.11 可移植性问题的一个例子
- 测试开发人均年薪30w+?软件测试工程师如何进阶拿到高薪?
- C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.2 Early Experience of C Language
- Review Set/Map basics with these two hooks
- groupByKey和reduceBykey的区别
猜你喜欢
随机推荐
Transformer学习
Appendix A printf, varargs and stdarg a. 2 use varargs. H to realize the variable argument list
JS hoisting: how to break the chain of Promise calls
方舟开服需要知道的那些事
左旋氧氟沙星/载纳米雄黄磁性/As2O3磁性Fe3O4/三氧化二砷白蛋白纳米球
51.【结构体初始化的两种方法】
Pytest: begin to use
基于php动漫周边商城管理系统(php毕业设计)
groupByKey和reduceBykey的区别
Appendix A printf, varargs and stdarg A.1 printf family of functions
空间数据库开源路,超图+openGauss风起禹贡
如何优雅的性能调优,分享一线大佬性能调优的心路历程
附录A printf、varargs与stdarg A.3 stdarg.h ANSI版的varargs.h
C专家编程 第1章 C:穿越时空的迷雾 1.3 标准I/O库和C预处理器
P7215 [JOISC2020] 首都 题解
Pagoda application experience
JS提升:如何中断Promise的链式调用
测试开发人均年薪30w+?软件测试工程师如何进阶拿到高薪?
pytest:开始使用
图片识别商品接口 API:天猫淘宝