当前位置:网站首页>【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.
边栏推荐
- Japan Sanitary Equipment Industry Association: Japan's warm water shower toilet seat shipments reached 100 million sets
- Mysql为什么 建立数据库失败
- 《PyTorch深度学习实践》第十课(卷积神经网络CNN)
- Advanced Redis
- RK3568环境安装
- typescript64-映射类型
- 在小程序中关于js数字精度丢失的解决办法
- Pytorch distributed parallel processing
- 1、Citrix XenDesktop 2203之AD域系统安装(一)
- [Tool Configuration] Summary of Common Uses of VSCode
猜你喜欢
随机推荐
MySQL表操作练习
typescript68-索引查询类型(查询多个)
Freeswitch操作基本配置
JS控制只能输入数字并且最多允许小数点两位
概率与期望部分题解
武田公司2022财年第一季度业绩强劲;正稳步实现全年的管理层指引目标
技术分析模式(十一)如何交易头肩形态
Hong Kong International Jewellery Show and Hong Kong International Diamond, Gem and Pearl Show kick off
腾讯业务安全岗 IDP 谈话总结
对数据类型而言运算符无效。运算符为 add,类型为 text。
技术分析模式(九)三重顶部和底部
RNote108---Display the running progress of the R program
【instancetype类型 Objective-C】
typescript60-泛型工具类型(readonly)
2022杭电多校六 1007-Shinobu loves trip(同余方程)
八大排序之堆排序
HR:这样的简历我只看了5秒就扔了,软件测试简历模板想要的进。
【LeetCode】235.二叉搜索树的最近公共祖先
[Tool Configuration] Summary of Common Uses of VSCode
【JVM调优】Xms和Xmx为什么要保持一致









