当前位置:网站首页>[@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.
边栏推荐
- JSD - 2204 - Knife4j framework - processing - Day07 response results
- ISC2022 HackingClub白帽峰会倒计时1天!最全议程正式公布!元宇宙集结,精彩绝伦!
- 宝塔应用使用心得
- 图片识别商品接口 API:天猫淘宝
- C专家编程 第1章 C:穿越时空的迷雾 1.5 今日之ANSI C
- XSS漏洞
- C语言_typedef和结构体
- JSD-2204-Knife4j框架-处理响应结果-Day07
- Spark shuffle tuning
- Based on php Xiangxi tourism website management system acquisition (php graduation design)
猜你喜欢

(七)《数电》——CMOS与TTL门电路

虚拟内存与物理内存之间的关系

C语言_联合体共用体引入

线上一次JVM FullGC搞得整晚都没睡,彻底崩溃~

基于php酒店在线预定管理系统获取(php毕业设计)

LeetCode

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

小程序--分包

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

Jmeter combat | Repeated and concurrently grabbing red envelopes with the same user
随机推荐
File operations of WEB penetration
用户量大,Redis没法缓存响应,数据库宕机?如何排查解决?
The difference between groupByKey and reduceBykey
基于php在线考试管理系统获取(php毕业设计)
C专家编程 第1章 C:穿越时空的迷雾 1.3 标准I/O库和C预处理器
Based on php Xiangxi tourism website management system acquisition (php graduation design)
C Expert Programming Chapter 1 C: Through the Fog of Time and Space 1.5 ANSI C Today
sizeof的详细解说和与strlen的区别
2022-08-01 第五小组 顾祥全 学习笔记 day25-枚举与泛型
C专家编程 第1章 C:穿越时空的迷雾 1.4 K&R C
shell脚本
How to encapsulate the cookie/localStorage sessionStorage hook?
在Cesium中实现与CAD的DWG图叠加显示分析
Suggestions and answer 8.1 C traps and defect chapter 8
数据库练习
MySQL相关知识
C语言_联合体共用体引入
JVM内存结构详解
WEB渗透之SQL 注入
Shell编程之条件语句