当前位置:网站首页>[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.
边栏推荐
- (四)旋转物体检测数据roLabelImg转DOTA格式
- 一天学会从抓包到接口测试,通过智慧物业项目深度解析
- Falsely bamboo brother today and found a localization of API to use tools
- 游戏思考19:游戏多维计算相关:点乘、叉乘、点线面距离计算
- 栈与队列的基本介绍和创建、销毁、出入、计算元素数量、查看元素等功能的c语言实现,以及栈的压入、弹出序列判断,栈结构的链式表示与实现
- Redis进阶
- The NDK compiler so libraries
- 2022最强版应届生软件测试面试攻略
- Source code analysis of Nacos configuration service (full)
- (4) Rotating object detection data roLabelImg to DOTA format
猜你喜欢

typescript64-映射类型

After working for 3 years, I recalled the comparison between the past and the present when I first started, and joked about my testing career

不太会讲爱,其实已经偷偷幸福很久啦----我们的故事

TCP sticky packet unpacking problem + solution

给网站套上Cloudflare(以腾讯云为例)

MySQL:基础部分

Falsely bamboo brother today and found a localization of API to use tools

工作3年,回想刚入门和现在的今昔对比,笑谈一下自己的测试生涯

(2022杭电多校六)1012-Loop(单调栈+思维)

(2022杭电多校六)1010-Planar graph(最小生成树)
随机推荐
Database table insert data
蓝牙gap协议
【Go】IM系统Centrifugo
Week 8 Document Clustering(文本聚类)
Kioxia and Aerospike Collaborate to Improve Database Application Performance
2022起重机司机(限桥式起重机)考试题库及模拟考试
Advanced Redis
2022最强版应届生软件测试面试攻略
GAN生成动漫头像Pytorch
typescript65-映射类型(keyof)
golang-条件语句
浮点数基础知识
访问被拒绝:“microsoft.web.ui.webcontrols”的解决办法
(2022杭电多校六)1012-Loop(单调栈+思维)
TCP sticky packet unpacking problem + solution
Technical Analysis Mode (8) Double Top and Bottom
HR:这样的简历我只看了5秒就扔了,软件测试简历模板想要的进。
共享内存+inotify机制实现多进程低延迟数据共享
Vulnhub靶机:HA_ NARAK
Freeswitch操作基本配置