当前位置:网站首页>[instancetype type Objective-C]
[instancetype type Objective-C]
2022-08-05 07:10:00 【The wind in the morning】
1. What is instancetype?
For example, there is a Person class:
@interface Person : NSObject
@property NSString *name;
@property int age;
- (Person *)person; //According to the specification, there should be a class method with the same name to return an object of this class
@end
@implementation Person
++ (Person *)person
{
return [Person new];
}
@end
At this time, if I want to get a Person object, then:
#import
int main()
{
Person *p1 = [Person person];
}
A question, can this person() method be inherited by the subclass Student?The answer is yes!
At this time, I call the person() method:
Student *s1 = [Student person];
The returned object is not a Student!
Now, how to make this method call through Person, and return the Person object, call it through Student, and return the Student object?
1) + (Person *)person; Can the return value type of this method write (Person *) type, no, if you write it like this, you can only return (Person *).What to write, id type:
- (id)person;
2) However, this method returns the Person object, because: the method writes return [Person new];
Therefore, whether it is adjusted through the parent class, or throughThe subclass is called, and all the returned Person objects are!
How to make this method call through the parent class Person, and return the Person object, and call it through the subclass Student, and return the Student object?with self- (id)person
{
return [self new];
}
At this time, through Person, this self refers to the Person class, and through Student, this self refers to Studentkind.Then someone has to ask, doesn't self refer to the current object?That is in the object method, self refers to the current object, this is the class method, self refers to the current class!
3) The class method in the parent class, create a parent class object and return
a.If the return value type is written as the parent class type, then the subclass calls this method and gets the parent class pointer
b.The solution is to change the return value to id type
c.Inside the method, when creating an object, don't write it to death. Once it is written to death, the created object will be fixed, such as Person, no matter how it is adjusted, it will return a Person object!
How can this be done, which class to call the method, is to create an object of which class?Write the class name as self, and which class calls this method, self refers to which class.The object of which class is created!
At this point, you can call the sayHi and study methods
[p1 sayHi];
[s1 study];
It looks flawless, but there is actually a problem, for example:
[Person person];
What type does the return value of this method return?id type.
Then can I access it through an NSString *str pointer?Yes
NSString *str = [Person person];
The compiler will not report a warning, but in fact it is not allowed!
How to make the class method called by [Person person]; can only be accessed with the Person pointer?If you're not using a Person pointer, I at least give you a warning!
4) The return value of the method is of type id. The problem is that any pointer can receive the return value of this method, and the compiler does not even have a warning!
The problem here is that the return value of this method is of type id, + (id)person; And for the type of id, we said that it will be compiled and checked, no!
So, how to make this method call through the Person class and return the Person object type, if this method is called through the Student object, it returns the Student type!
Then write the return value as: instancetype
What does instancetype mean?The return value representing this method is the object of the current class!Which class to call this method is the object of which class!
5) If the return value of the method is instancetype, what does it mean? It means that the return value of the method is the object of the current class!
At this time, the return value of this method [Person person]; is the Person pointer type.
- (id)person
Second, usage suggestions
1. If the object of the current class is created inside the method, it is best not to write it as the class name, use self, that is, don't write [class name new];Because once you write it to death, can the subclass inherit the method, then once the subclass is inherited, the effect of the subclass may not be the same.
Use self instead of class name, [self new];
2. If the return value of the method is an object of the current class, you should not write it to death, but use instancetype instead. What does instancetype mean, the current classObject!Whoever calls this method represents whose object!
The difference between 3.id and instancetype
1) id is a type, instancetype can only be used as the return value of the method, and cannot be used elsewhere;
id can be used to declare a pointer variable, as a parameter, or as a return value
2) instancetype is a typed thing that represents the object of the current class. For example, if I call this method in Person, then instancetype represents Person *. If I call this method in Student, then instancetype represents Student *. If this methodI call it in NSString, then instancetype represents NSString *
id is an untyped pointer.Just an address, no typed pointer.
边栏推荐
- MyCat配置文件
- Takeda Fiscal 2022 First Quarter Results Strong; On Track to Achieve Full-Year Management Guidance
- MySQL的主从模式搭建
- After the firewall iptable rule is enabled, the system network becomes slow
- C# FileSystemWatcher
- 今天虚竹哥又发现了一款好用的国产化API工具
- 性能提升400倍丨外汇掉期估值计算优化案例
- Week 8 Document Clustering(文本聚类)
- Libpq 是否支持读写分离配置
- MySQL表操作练习
猜你喜欢
IO process thread -> communication between processes -> day7
Shared memory + inotify mechanism to achieve multi-process low-latency data sharing
2022杭电多校六 1007-Shinobu loves trip(同余方程)
给网站套上Cloudflare(以腾讯云为例)
【5】Docker中部署MySQL
【网友真实投稿】为女友放弃国企舒适圈,转行软件测试12k*13薪
二叉搜索树问题
(4) Rotating object detection data roLabelImg to DOTA format
protobuf根据有关联的.proto文件进行编译
Flink学习11:flink程序并行度
随机推荐
线程池的使用(结合Future/Callable使用)
(JLK105D)中山爆款LED恒流电源芯片方案
typescript62-泛型工具类型(record)
技术分析模式(九)三重顶部和底部
typescript68-索引查询类型(查询多个)
【工具配置篇】VSCode 常用使用总结
Technical Analysis Mode (8) Double Top and Bottom
MySQL的主从模式搭建
开启防火墙iptable规则后,系统网络变慢
边缘盒子+时序数据库,美的数字化平台 iBUILDING 背后的技术选型
export使用
怎么样避免线上内存泄漏
2022最强版应届生软件测试面试攻略
How to avoid online memory leaks
mysql使用in函数的一个小问题
武田公司2022财年第一季度业绩强劲;正稳步实现全年的管理层指引目标
铠侠携手Aerospike提升数据库应用性能
typescript64-映射类型
Libpq 是否支持读写分离配置
360度反馈调查表中的问题示范