当前位置:网站首页>【Dynamic type detection Objective-C】
【Dynamic type detection Objective-C】
2022-08-05 07:09:00 【morning breeze】
Foreword
1) What is the name of the compiler in Objective-C, LLVM, it can compile C language, Objective-C language, Swift language, C++ language.
2) When the compiler is compiling, it judges whether a pointer can call the method of the object pointed to, its judgment criterion is the type of the pointer,
So, at this time, we can easily compileThe compiler cheated:
NSString *str = @"jack";
[str sayHi];
Well, let's say, the compiler will report an error at this time!We want to deceive the compiler, how to deceive it, is it to force it into Person * type?
[(Person *)str sayHi];
At this time, the compiler will not report an error.
3) Even if you fool the compiler, it will do the running check again.
Therefore, even if the program we write passes the compilation, it does not mean that it can be executed perfectly!
4) So, before executing the sayHi(); method, can I judge by myself whether there is a sayHi(); method in the object pointed to by the str pointer?
How to write the code, let’s judge firstLet's see if there is this method in the object. If so, execute it. If not, don't execute it.This can be avoided, because there is no way, and report an error!
First, how to judge whether there is such a method?
1) Determine whether this method can be executed in the object pointed to by the pointer.
Syntax:
Person *p1 = [Person new];
[p1 respondsToSelector:@selector(sayHi)];
It will help me determine whether the p1 object can respond to the sayHi method,The implication is that it will judge whether there is this sayHi method in the p1 object.
What type of return value, BOOL
BOOL b1 = [p1 respondsToSelector:@selector(sayHi)];
If the return value is YES, should I just call it at this time?
if(b1 == YES)
{
[p1 sayHi];
}
What about the length method?
BOOL b1 = [p1 respondsToSelector:@selector(length)];
if(b1 ==YES)
{
[p1 sayHi];
}
else
{
NSLog(@"NO...");
}
–(BOOL)respondsToSelector:(SEL);
Second, isKindOfClass method
1.Syntax
– (BOOL) isKindOfClass:(Class)aClass;
1) What does this method mean:
Determine whether an object is an instance of a specified class, or an instance of a subclass of this specified class.
For example: [p1 isKindOfClass:[Person class]];
BOOL b2 = [p1 isKindOfClass:[Person class]];
NSLog(@"b2= %d",b2);//answerIs 1
What if it is NSObject?
BOOL b2 = [p1 isKindOfClass:[NSObject class]];
NSLog(@“b2= %d”,b2);//The answer is still 1
If it is NSString, the answer is 0
If it says Student, the answer is 0
If yes:
Student *s1 = [Student new];
BOOL b2 = [s1 isKindOfClass:[Student class] ];
NSLog(@"b2 =%d",b2);//The answer is 1
If yes:
Student *s1 = [Student new];
BOOL b2 = [s1 isKindOfClass:[Person class] ];
NSLog(@"b2 = %d",b2);
Determine whether s1 is a Person object, or a subclass object of Person
2.isMemberOfClass
– (BOOL)isMemberOfClass:(Class)class;
Example:
Student *s1 = [Student new];
BOOL b2 = [s1 isMemberOfClass:[Person class]];
NSLog(@"b2 = %d",b2);
The answer is 0
Example:
Student *s1 = [Student new];
BOOL b2 = [s1 isMemberOfClass:[Student class]];
NSLog(@"b2 = %d",b2);
The answer is 1
The function of isMemberOfClass is to judge whether the s1 object is a Student object,
Remember, it does not include its subclasses.
Only this class can be judged, not including subclasses.
Determines whether the object is an object of the specified class, excluding subclasses.
isSubclassOfClass
++(BOOL)isSubclassOfClass:(Class)aClass;
Determine whether the specified class is a subclass of another class
For example:
BOOL b3 = [Student isSubclassOfClass:[Person class]];
NSLog(@"b3 = %d",b3);
The answer is 1
Determine whether the Student class is a subclass of the Person class.
The above are the four methods of dynamic type detection, the most commonly used are:
– (BOOL) respondsToSelector: (SEL) aSelector; this method
Before calling an object method, we generally judge firstIs there any object method in this object?
Then if the object I created is a
Person object, I still need to judge whether there is a sayHi method in this object, no!
Where is this respondsToSelector used?
Later, when we call a method written by someone else, that method will return an object to me. At this time, you may not know what the object is. It may return an id type, then thisWhether there is such a method in the object, so, before you adjust, what is the best thing to do first, to judge!
Remember, this respondsToSelector can only judge the object method, that is, whether the object has this object method;
Then I want to judge whether there is this class method in the class, use:
BOOL b4 = [StudentinstancesRespondToSelector:@selector(sayHi)];
NSLog(@"b4 = %d",b4);
It will judge whether there is a class method sayHi in the Student class
++ (BOOL)instancesRespondToSelector:(SEL)aSelector;
Determines whether there is a specified class method in the class.
边栏推荐
- Hong Kong International Jewellery Show and Hong Kong International Diamond, Gem and Pearl Show kick off
- 小程序input框不允许输入负数
- Technical Analysis Mode (7) Playing the Gap
- 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
- 2022 Fusion Welding and Thermal Cutting Operation Certificate Exam Questions and Mock Exams
- 在小程序中关于js数字精度丢失的解决办法
- 微信小程序仿input组件、虚拟键盘
- 八大排序之堆排序
- 开启防火墙iptable规则后,系统网络变慢
- Put Cloudflare on the website (take Tencent Cloud as an example)
猜你喜欢
随机推荐
Shiny04---Application of DT and progress bar in shiny
腾讯实习总结
Flink学习10:使用idea编写WordCount,并打包运行
2022最强版应届生软件测试面试攻略
今天虚竹哥又发现了一款好用的国产化API工具
技术分析模式(八)双顶和底
开源中国活动合作说明书
Hong Kong International Jewellery Show and Hong Kong International Diamond, Gem and Pearl Show kick off
typescript66-分析partial的实现
工作3年,回想刚入门和现在的今昔对比,笑谈一下自己的测试生涯
Source code analysis of Nacos configuration service (full)
专用机终端安装软件后报IP冲突
数据库多表关联插入数据
binary search tree problem
【C语言】结构体变量数据通过 void* 传入到函数中
(2022杭电多校六)1012-Loop(单调栈+思维)
利用将网页项目部署到阿里云上(ngnix)
After the firewall iptable rule is enabled, the system network becomes slow
蓝牙gap协议
MySQL: JDBC programming