当前位置:网站首页>[@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.
边栏推荐
- 怎么做postgrsql主备?
- 236. 二叉树的最近公共祖先
- 超级复杂可贴图布局的初级智能文本提示器
- ”QSqlDatabasePrivate::removeDatabase: connection ‘test-connect‘ is still in use“数据库多次打开报错
- The cornerstone of high concurrency: multithreading, daemon threading, thread safety, thread synchronization, mutual exclusion lock, all in one article!...
- 无法启动服务 错误 193 0xc1
- Brute force recursion to dynamic programming 07 (516. Longest palindrome subsequence)
- js垃圾回收机制
- 扩展卡尔曼滤波【转】
- 新库上线 | CnOpenDataA股上市公司董监高信息数据
猜你喜欢

Excel 如何比较两列字符串是否相同?

【Swoole系列3.3】单进程管理Process

SAP ABAP OData 服务如何支持修改(Update)操作试读版

暴力递归到动态规划 08(小马走象棋)

.NET in-depth analysis of the LINQ framework (four: IQueryable, IQueryProvider interface details)

扩展卡尔曼滤波【转】

lombok 下的@Builder和@EqualsAndHashCode(callSuper = true)注解

Incorrect datetime value: ‘2022-01-01‘ for function str_to_date

Shell脚本乘法口诀等小实验

FLIR E95 在8层楼看马路上行驶的CAR的热成像形态?
随机推荐
公司代码学习笔记
粘包与拆包
【面经】被虐了之后,我翻烂了equals源码,总结如下
MySQL删库不跑路
扩展卡尔曼滤波【转】
可信的SSL证书颁发机构有哪些
OpenWRT设置ipv6网络
爆款视频怎么做?这里或许有答案
EasyGBS播放器优化:设备通道视频播放出现跳屏问题的修复
236. 二叉树的最近公共祖先
Go高性能之方法接收器 - 指针vs值
企业云成本管控,你真的做对了吗?
Incorrect datetime value: ‘2022-01-01‘ for function str_to_date
为什么要使用 playwright 做浏览器自动化测试?
吴恩达深度学习deeplearning.ai——第一门课:神经网络与深度学习——第一节:深度学习概论
常用工具链和虚拟环境-TDMGCC
Topic Modeling of Short Texts: A Pseudo-Document View
ssh(sshd)安全配置
11-security认证.md
lombok 下的@Builder和@EqualsAndHashCode(callSuper = true)注解