当前位置:网站首页>About static type, dynamic type, ID, instancetype
About static type, dynamic type, ID, instancetype
2022-07-06 18:51:00 【InfoQ】
Static type
- When a pointer variable is defined as a specific object , Using static types , The class of this pointer variable is known at compile time , This variable always stores objects of a specific class , By default, all data types are static
@interface Person : NSObject
property (nonatomic,strong) NSString *name;
-(void)run;
@end
@implementation Person
-(void)run{
NSLog(@"run");
}
@end
int main(int argc, const char * argv[]) {
Person *p = [[Person alloc]init];
p.name = @"ABC";
[p run];
return 0;
}
- Characteristics of static data types :
- You know the type of variable at compile time
You know it at compile time p yes Person type
- Know which properties and methods are in the variable
@property (nonatomic,strong) NSString *name;
-(void)run;
- These properties and methods can be accessed at compile time
p.name = @"ABC";
[p run];
- And if you define variables through static data types , If you access properties and methods that are not static data types , Then the compiler will report an error

Dynamic type
- It refers to the class that the program does not determine the object belongs to until it is executed
@interface Person : NSObject
@property (nonatomic,strong) NSString *name;
-(void)run;
@end
@implementation Person
-(void)run{
NSLog(@"run");
}
-(void)test{
NSLog(@"test");
}
@end
@interface Student : Person
@property (nonatomic,assign) int age;
-(void)eat;
@end
@implementation Student
-(void)eat{
NSLog(@"eat");
}
@end
int main(int argc, const char * argv[]) {
id obj1 = [[Person alloc]init];
[obj1 run];
id obj2 = [[Student alloc]init];
[obj2 eat];
return 0;
}
- Characteristics of dynamic data types :
- At compile time, the compiler doesn't know the real type of the variable , The real type of it is known only at run time
id obj1 = [[Person alloc]init];
id obj2 = [[Student alloc]init];
id
The type and
instancetype
idtype
idIs a common object type , It can point to objects belonging to any class , It can be understood as a universal pointer , amount to C Linguisticvoid*data type ,idIt is also a dynamic data type , Can be used to define variables , As a function parameter , Return value as a function, etc
id == NSObject*( Universal pointer )
NSObject*It's a static type
- Because dynamic data types can call any method , So it's possible to call methods that don't belong to you , And no error is reported during compilation , So it leads to runtime errors
- Use scenarios : polymorphic , Can reduce the amount of code , To avoid calling subclass specific methods, you need to force type conversion
- id Type cannot use point syntax , Because point syntax is a feature of the compiler , and id Types are runtime characteristics
- Try to use static types , Static types can detect errors earlier , Improve readability
instancetype
instancetypeThe true type of the object can be determined at compile time
instancetypeCan only be used for return values
- Custom construction method , Use the return value as much as possible
instancetype, Do not useid
边栏推荐
- Afnetworking framework_ Upload file or image server
- QLabel 跑马灯文字显示
- A method of sequentially loading Unity Resources
- Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
- POJ 2208 已知边四面体六个长度,计算体积
- Hongke shares | plate by plate ar application in Beijing Winter Olympics
- Understanding disentangling in β- VAE paper reading notes
- Splay
- 287. 寻找重复数
- SQL injection - access injection, access offset injection
猜你喜欢

Reptiles have a good time. Are you full? These three bottom lines must not be touched!
![[depth first search] Ji suanke: Square](/img/fc/e42ae0d036be258bed5623d55fc2db.jpg)
[depth first search] Ji suanke: Square

287. Find duplicates

Solve DoS attack production cases

手写一个的在线聊天系统(原理篇1)

同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
![[Matlab] Simulink 同一模块的输入输出的变量不能同名](/img/99/adfe50075010916439cd053b8f04c7.png)
[Matlab] Simulink 同一模块的输入输出的变量不能同名

The role of applet in industrial Internet

Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法

44所高校入选!分布式智能计算项目名单公示
随机推荐
Markdown syntax for document editing (typera)
文档编辑之markdown语法(typora)
AcWing 3537.树查找 完全二叉树
基于ppg和fft神经网络的光学血压估计【翻译】
测试行业的小伙伴,有问题可以找我哈。菜鸟一枚~
Cocos2d Lua smaller and smaller sample memory game
使用map函数、split函数一行键入多个元素
使用cpolar建立一个商业网站(1)
Reproduce ThinkPHP 2 X Arbitrary Code Execution Vulnerability
能源行业的数字化“新”运维
Hongke shares | plate by plate ar application in Beijing Winter Olympics
Use cpolar to build a business website (1)
C#/VB. Net to add text / image watermarks to PDF documents
抽象类与抽象方法
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
Penetration test information collection - CDN bypass
Interpreting cloud native technology
Self supervised heterogeneous graph neural network with CO comparative learning
QPushButton绑定快捷键的注意事项
Implementation of AVL tree