当前位置:网站首页>[@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.
边栏推荐
- Shell编程条件语句
- Chapter 12, target recognition of digital image processing
- 方舟开服需要知道的那些事
- C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.3 The Standard I/O Library and the C Preprocessor
- 基于php动漫周边商城管理系统(php毕业设计)
- 2022-08-01 第五小组 顾祥全 学习笔记 day25-枚举与泛型
- C陷阱与缺陷 第7章 可移植性缺陷 7.9 大小写转换
- How to encapsulate the cookie/localStorage sessionStorage hook?
- 可视化——Superset使用
- 上传markdown文档到博客园
猜你喜欢

ORI-GB-NP半乳糖介导冬凌草甲素/姜黄素牛血清白蛋白纳米粒的研究制备方法

CS-NP白蛋白包覆壳聚糖纳米颗粒/人血清白蛋白-磷酸钙纳米颗粒无机复合材料

Based on php film and television information website management system acquisition (php graduation design)

【中文树库标记---CTB】

WEB 渗透之文件类操作

基于php在线音乐网站管理系统获取(php毕业设计)

基于php旅游网站管理系统获取(php毕业设计)

2022-08-01 第五小组 顾祥全 学习笔记 day25-枚举与泛型

TP5-NPs负载噻吩类化合物TP5白蛋白纳米粒/阿魏酸钠新糖牛血清蛋白纳米粒

对C语言结构体内存对齐的理解
随机推荐
PyQt5 + MySQL5.8 【学生信息管理系统】【增删改查】
图的邻接矩阵存储
C陷阱与缺陷 第8章 建议与答案 8.2 答案
C Pitfalls and Defects Chapter 7 Portability Defects 7.9 Case Conversion
基于php动漫周边商城管理系统(php毕业设计)
方舟:生存进化PVE模式和PVP模式
数据库练习
测试的意义并不是能找到全部的缺陷
【中文树库标记---CTB】
二分法中等 LeetCode6133. 分组的最大数量
C expert programming
测试开发人均年薪30w+?软件测试工程师如何进阶拿到高薪?
LVS负载均衡群集
Based on php film and television information website management system acquisition (php graduation design)
基于php在线学习平台管理系统获取(php毕业设计)
C专家编程 第1章 C:穿越时空的迷雾 1.4 K&R C
51.【结构体初始化的两种方法】
NFT的10种实际用途(NFT系统开发)
AIDL通信
包含吲哚菁绿的多聚体白蛋白纳米球/载马钱子碱纳米粒的牛血清白蛋白微球的制备