当前位置:网站首页>[@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.
边栏推荐
- 一个人的精力
- 易购数码类电商商城网页设计与实现项目源码
- 吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第二节:神经网络基础(上)
- 可信的SSL证书颁发机构有哪些
- 暴力递归到动态规划 06 (剑指 Offer II 095. 最长公共子序列)
- 【7.31】代码源 - 【矩阵操作】【宝箱】【New Stone Game】【等差数列】
- .NET in-depth analysis of the LINQ framework (four: IQueryable, IQueryProvider interface details)
- pytorch 中 permute()函数的用法
- 12-security退出.md
- 关于提高企业网络安全意识
猜你喜欢
随机推荐
自己做的选择
apache-activemq-5.14.1
一个人的精力
什么情况下DigiCert证书会引起发生安全警报?
写一个简单的网站步骤
[Example构造方法增加notNull参数,默认false,允许值为null,值为null的时候不加入到条件中
12-security退出.md
MySQL删库不跑路
qt opengl 使用不同的颜色绘制线框三角形
YYGH-BUG-06
numpy PIL tensor之间的相互转换
【面经】被虐了之后,我翻烂了equals源码,总结如下
VS Code 这么牛,再次印证了一句名言
超级复杂可贴图布局的初级智能文本提示器
Greenplum database failure analysis, can not listen to the port
openCV第二篇
优秀的 Verilog/FPGA开源项目总结及交流群
PHICOMM(斐讯)N1盒子 - Armbian5.77(Debian 9)配置自动连接WIFI无线网络
暴力递归到动态规划 08(小马走象棋)
如何准备考pmp?









