当前位置:网站首页>【Objective-C中的@synthesize】
【Objective-C中的@synthesize】
2022-08-01 21:19:00 【清风清晨】
前言
@property只能生成getter和setter方法的声明,那实现怎么办呢?
一、@synthesize是什么?
比如,有一个类
#import <Foundation/Foundation.h>
@interface Person : NSObject
{
NSString *_name;
int _age;
float _weight;
}
@property NSString *name;
@property int age;
@property float weight;
@end
@implementation Person
@end
那么,getter和setter方法的实现,能不能自动生成呢?
1)@synthesize的作用:自动生成getter和setter方法的实现。
2)语法:@synthesize @property的名称;
例如:
@interface Person : NSObject
{
int _age;
}
@property int age;
@end
----------------------
@implementation Person
@synthesize age;
@end
二、@synthesize做的事情
1.生成一个真私有的属性
1) 什么叫真私有的属性,也就是声明在@implementation之中的,
2)这个属性的类型和@synthesize对应的@property类型一致;
3)属性的名字和@synthesize对应的@property名字一致;
例如,这段代码:
@implementation Person
@synthesize age;
@end
转换为
@implementation Person
{
int age;
}
- (void)setAge:(int)age;
{
self->age = age;
} - (int)age
{
return age;
}
@end
4)自动生成setter方法的实现
实现的方式:直接把参数age的值传递给它生成的那个私有属性age;
5)自动生成getter方法的实现
实现的方式:返回它自动生成的那个私有属性的值
2.如何让@synthesize不要自动生成私有属性,就用我们自己定义的带下划线的属性,就行了
语法:@synthesize @property名称 = 已经存在的属性名;
例如:@synthesize age = _age;
那么,这句话它是什么意思呢?
1)不再生成私有属性;
2)直接生成getter和setter方法的实现;
3)setter怎么实现的呢?把参数的值赋值给已经存在的下划线属性
例如:
- (void)setAge:(int)age
{
_age = age;
}
4)getter怎么实现的呢?直接返回已经存在的下划线属性的值。
- (int)age
{
return _age;
}
总结
使用@synthesize,有几点需要注意:
1)如果直接写1个@synthesize
例如:@synthesize name;
那么,它自动生成一个私有属性,并且操作的是自动生成的私有属性
2)如果指定操作的属性
例如:@synthesize name = _name;
那么,它就不会自动生成私有属性,并且操作的是我们指定的带下划线的属性;
3)生成的setter方法实现当中,是没有做任何逻辑验证的,是直接赋值。如果要实现逻辑验证,需要自己写setter方法的实现。例如:
- (void)setAge:(int)age
{
if(age >= 0 && age <= 120)
{
_age = age;
}
else
_age = 18;
}
4)批量声明
例如:
@property float height , weight;
如果多个@property的类型是一致的,可以批量声明;
@synthesize name, age, weight, height;
@synthesize name = _name,age=_age,weight=_weight,height= _height;
@synthesize类型不一致,也是可以批量声明的。
边栏推荐
- C Pitfalls and Defects Chapter 7 Portability Defects 7.11 An Example of a Portability Problem
- Realize the superposition display analysis of DWG drawing with CAD in Cesium
- 那些关于DOM的常见Hook封装(二)
- MySQL 中出现的字符编码错误 Incorrect string value: ‘\x\x\x\x‘ for column ‘x‘
- TP5-NPs负载噻吩类化合物TP5白蛋白纳米粒/阿魏酸钠新糖牛血清蛋白纳米粒
- C Pitfalls and Defects Chapter 7 Portability Defects 7.9 Case Conversion
- 使用员工管理软件,解锁人力生产力新水平,提高人力资源团队灵活性
- XSS漏洞
- How to choose Visibility, Display, and Opacity when interacting or animating
- 测试开发人均年薪30w+?软件测试工程师如何进阶拿到高薪?
猜你喜欢

响应式织梦模板清洁服务类网站

30+的女性测试人面试经验分享

【接口测试】JMeter调用JS文件实现RSA加密

PyQt5 + MySQL5.8 【学生信息管理系统】【增删改查】

Questions I don't know in database kernel interview(1)

牛血清白蛋白刺槐豆胶壳聚糖缓释纳米微球/多西紫杉醇的纳米微球DTX-DHA-BSA-NPs

Jmeter实战 | 同用户重复并发多次抢红包

使用百度EasyDL实现厂区工人抽烟行为识别

ISC2022 HackingClub white hat summit countdown 1 day!Most comprehensive agenda formally announced!Yuan universe, wonderful!

【中文树库标记---CTB】
随机推荐
空间数据库开源路,超图+openGauss风起禹贡
ahooks 是怎么处理 DOM 的?
C陷阱与缺陷 第5章 库函数 5.5 库函数signal
C pitfalls and pitfalls Chapter 8 Suggestions and answers 8.2 Answers
C语言_typedef和结构体
附录A printf、varargs与stdarg A.2 使用varargs.h来实现可变参数列表
【力扣】字符串相乘
MySQL 中出现的字符编码错误 Incorrect string value: ‘\x\x\x\x‘ for column ‘x‘
C Pitfalls and Defects Chapter 7 Portability Defects 7.7 Truncation During Division
使用百度EasyDL实现厂区工人抽烟行为识别
JSD-2204-Knife4j框架-处理响应结果-Day07
ISC2022 HackingClub white hat summit countdown 1 day!Most comprehensive agenda formally announced!Yuan universe, wonderful!
Nacos 配置中心
C Pitfalls and Defects Chapter 7 Portability Defects 7.6 Memory Location 0
【中文树库标记---CTB】
How to make the timer not execute when the page is minimized?
pytest:开始使用
这些 hook 更优雅的管理你的状态
牛血清白蛋白-葡聚糖-叶黄素纳米颗粒/半乳糖白蛋白磁性阿霉素纳米粒的制备
RecycleView的使用