当前位置:网站首页>[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.
边栏推荐
猜你喜欢
随机推荐
不太会讲爱,其实已经偷偷幸福很久啦----我们的故事
【2022 DSCTF决赛wp】
二叉搜索树问题
武田公司2022财年第一季度业绩强劲;正稳步实现全年的管理层指引目标
Technical Analysis Mode (7) Playing the Gap
Rapid Medical超小体积且唯一可调的取栓器获得FDA核准
typescript65-映射类型(keyof)
(2022杭电多校六)1012-Loop(单调栈+思维)
今天虚竹哥又发现了一款好用的国产化API工具
技术分析模式(九)三重顶部和底部
h5页面回退到微信小程序并携带参数
真实字节跳动测试开发面试题,拿下年薪50万offer。
Shiny02---Shiny异常解决
2022杭电多校六 1007-Shinobu loves trip(同余方程)
export使用
How to avoid online memory leaks
《基于R语言的自动数据收集》--第3章 XML和JSON
腾讯业务安全岗 IDP 谈话总结
技术分析模式(七)发挥差距
【网友真实投稿】为女友放弃国企舒适圈,转行软件测试12k*13薪









