当前位置:网站首页>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
id
type
id
Is 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 ,id
It 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
instancetype
The true type of the object can be determined at compile time
instancetype
Can only be used for return values
- Custom construction method , Use the return value as much as possible
instancetype
, Do not useid
边栏推荐
- Automatic reservation of air tickets in C language
- DOM简要
- 朗坤智慧冲刺科创板:年营收4亿 拟募资7亿
- atcoder它A Mountaineer
- openmv4 学习笔记1----一键下载、图像处理背景知识、LAB亮度-对比度
- Some understandings of tree LSTM and DGL code implementation
- Summary of performance knowledge points
- Use map function and split function to type multiple elements in one line
- 监控界的最强王者,没有之一!
- Shangsilicon Valley JUC high concurrency programming learning notes (3) multi thread lock
猜你喜欢
AvL树的实现
CSRF vulnerability analysis
Binary search tree
44 colleges and universities were selected! Publicity of distributed intelligent computing project list
Mathematics in machine learning -- common probability distribution (XIII): Logistic Distribution
抽象类与抽象方法
测试行业的小伙伴,有问题可以找我哈。菜鸟一枚~
44所高校入选!分布式智能计算项目名单公示
helm部署etcd集群
Method of accessing mobile phone storage location permission under non root condition
随机推荐
Jdbc driver, c3p0, druid and jdbctemplate dependent jar packages
Picture zoom Center
If you have any problems, you can contact me. A rookie ~
图之广度优先遍历
朗坤智慧冲刺科创板:年营收4亿 拟募资7亿
UFIDA OA vulnerability learning - ncfindweb directory traversal vulnerability
44 colleges and universities were selected! Publicity of distributed intelligent computing project list
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
None of the strongest kings in the monitoring industry!
关于npm install 报错问题 error 1
About NPM install error 1
epoll()无论涉及wait队列分析
测试行业的小伙伴,有问题可以找我哈。菜鸟一枚~
随着MapReduce job实现去加重,多种输出文件夹
美庐生物IPO被终止:年营收3.85亿 陈林为实控人
【LeetCode第 300 场周赛】
Penetration test information collection - site architecture and construction
[matlab] Simulink the input and output variables of the same module cannot have the same name
How are you in the first half of the year occupied by the epidemic| Mid 2022 summary
First, look at K, an ugly number