当前位置:网站首页>[@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.
边栏推荐
- How to encapsulate the cookie/localStorage sessionStorage hook?
- 包含吲哚菁绿的多聚体白蛋白纳米球/载马钱子碱纳米粒的牛血清白蛋白微球的制备
- 2022-08-01 第五小组 顾祥全 学习笔记 day25-枚举与泛型
- LeetCode·每日一题·1374.生成每种字符都是奇数个的字符串·模拟
- XSS漏洞
- render-props和高阶组件
- 树莓派的信息显示小屏幕,显示时间、IP地址、CPU信息、内存信息(c语言),四线的i2c通信,0.96寸oled屏幕
- Review Set/Map basics with these two hooks
- Taobao's API to get the list of shipping addresses
- Transplant MQTT source code to STM32F407 development board
猜你喜欢
render-props和高阶组件
LeetCode·每日一题·1374.生成每种字符都是奇数个的字符串·模拟
线上一次JVM FullGC搞得整晚都没睡,彻底崩溃~
Anacoda的用途
基于php在线考试管理系统获取(php毕业设计)
Upload markdown documents to blog garden
对C语言结构体内存对齐的理解
Realize the superposition display analysis of DWG drawing with CAD in Cesium
How to choose Visibility, Display, and Opacity when interacting or animating
Jmeter实战 | 同用户重复并发多次抢红包
随机推荐
PX4模块设计之十五:PX4 Log设计
数据库练习
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
C陷阱与缺陷 第7章 可移植性缺陷 7.8 随机数的大小
Pytest: begin to use
方舟生存进化是什么游戏?好不好玩
软考 ----- UML设计与分析(上)
ISC2022 HackingClub white hat summit countdown 1 day!Most comprehensive agenda formally announced!Yuan universe, wonderful!
Popular explanation: what is a clinical prediction model
位运算简介
回收租凭系统100%开源无加密 商城+回收+租赁
C Pitfalls and Defects Chapter 7 Portability Defects 7.7 Truncation During Division
C Expert Programming Preface
shell脚本
深拷贝浅拷贝
宝塔应用使用心得
小程序--分包
C语言_联合体共用体引入
Image fusion GANMcC study notes