当前位置:网站首页>[@property enhancement in Objective-C language]
[@property enhancement in Objective-C language]
2022-08-03 03:02:00 【morning breeze】
Foreword
What are @property enhancements?
Writing before Xcode 4.4:
1) @property only generates the declaration of getter and setter;
@synthesize only generates the implementation of getter and setter;
properties have to be written by themselves;
This way of writing is before Xcode4.4. After Xcode4.4, Xcode made an enhancement to @property
1. What does @property enhance?
After Xcode4.4, you only need to write a @property, it will:
1) automatically generate private properties for you,
2) help you generate getter and setter declarations,
3)Helps you generate getter and setter implementations.
Second, use steps
1. For example, there is such a class:
@interface Student : NSObject
@end
@implementation Student
@end
1) I don't do anything, I just write a statement like this:
@interface Student : NSObject
@property NSString *name;
@end
2) It already does it for youThere are many things:
a) Automatically generate a private property for you: the type of the property is the same as the type of @property, the name of the property is the same as the name of @property, and an underscore is automatically added in front;
b) AutomaticallyHelp you to generate the declaration of the getter and setter method of this property;
c) Automatically help you generate the implementation of the getter and setter method of this property:
Implementation of the setter: directly assign the value of the parameter to the automatically generated privateProperty;
The implementation of getter: directly return the value of the generated private property;
2. Use Note:
1) The type of @property must be the same as the type of the property to be generated;
2) The name of @property must be the same as the name of the property, just remove the underscore;
For example: @property intage;
3) @property can also be declared in batches; the types must be the same;
For example: @property float height, weight;
4) @property automatically generates method implementation without any logic verification;
setter: direct assignment;
getter: direct return;
5) If you want to do logic verification, just rewrite the setter method, for example:
(void)setAge:(int)
{
if(age >= 0 && age <= 120)
{
_age = age;
}
}
After you override the setter method, will the getter method be automatically generated?The answer is: yes!
If you rewrite the getter, the setter will be automatically generated;
If you rewrite the getter and the setter at the same time, @property will not automatically generate the private property.then what should we do?Declare private properties in @implementation yourself!
6) From today, if you want to write 1 property for the class, and encapsulate the setter and getter for this property, 1 @property will do it.
7) Can this @property be inherited?
The answer is: it can be inherited by subclasses, but since the properties generated by @property are private properties, they cannot be directly accessed by subclasses. If you must access them, they can only be accessed through setter and getter methods.
For example, there is an Animal class:
@interface Animal : NSObject
@property NSString *name;
@end
At this time, an underlined name of type NSString * is automatically generatedPrivate properties, and getter and setter methods are automatically generated.
At this time, there is a Pig class that inherits from the Animal class:
@interface Pig : Animal@end
At this time, does the Pig class inherit from the Animal class?Can it inherit the _name of type Animal's private property NSString *?The answer is: yes.Can getter and setter methods be inherited?The answer is: yes.
However, in the method implementation of the Pig class, can it access the _name attribute through self->_name?The answer is: no.
@implementation Pig- (void)test
{
self->
}
@end
This is not prompted and cannot be accessed.Can't access, why?Private, invisible.
But how can you access it, by calling the setter method and getter method through the super keyword, for example:
@implementation Pig - (void)test
{
[super setName:@“jack”];
}
@end
You can also access the setter and getter methods through the self keyword, for example:
@implementation Pig - (void)test
{
[self setName:@“jack”];
}
@end
Of course, you can also access setter and getter methods by calling the self keyword and dot syntax, for example:
@implementation Pig - (void)test
{
self.name = @"jack";
}
@end
- (void)test
Summary
At this point, @property is officially over, 3 articles have been written, and this @property has nothing else.
边栏推荐
猜你喜欢
有趣简单的M2处理器性能实验:Swift与C代码执行速度的比较
Violence recursion to dynamic programming 08 (pony go chess)
增删改查这么多年,最后栽在MySQL的架构设计上!
10. SAP ABAP OData 服务如何支持修改(Update)操作
国标GB28181协议EasyGBS平台项目现场通知消息过多导致系统卡顿该如何解决?
大厂标配 | 百亿级并发系统设计 | 学完薪资框框涨
【SQL】—数据库操作、表操作
mysql容器数据卷持久化
Violent recursion to dynamic programming 06 (the sword refers to Offer II 095. Longest common subsequence)
Greenplum database failure analysis, can not listen to the port
随机推荐
mysql容器数据卷持久化
个人开发者必备,免费 API 网关工具推荐
mysql binlog日期解析成yyyy-MM-dd
买了一瓶饮料
Greenplum数据库故障分析——can not listen port
优秀的 Verilog/FPGA开源项目总结及交流群
南瓜科学新品上线 开辟益智玩具新世界
[NCTF2019]SQLi-1||SQL Injection
Violent recursion to dynamic programming 06 (the sword refers to Offer II 095. Longest common subsequence)
如何让优炫数据库开机自启
怎么从零编写一个 v3 版本的 chrome 浏览器插件实现 CSDN 博客网站的暗黑和明亮主题切换?
Qt在选择MSVC 编译器的时候,无法识别出M_PI的问题处理
增删改查这么多年,最后栽在MySQL的架构设计上!
44LVS负载均衡群集-NAT
大厂标配 | 百亿级并发系统设计 | 学完薪资框框涨
flask-socketio实现websocket通信
扩展卡尔曼滤波【转】
怎么做postgrsql主备?
ssh(sshd)安全配置
MySQL里获取当前周、月、季的第一天/最后一天