当前位置:网站首页>【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.
边栏推荐
猜你喜欢
随机推荐
UDP broadcast
IO process thread -> communication between processes -> day7
合工大苍穹战队视觉组培训Day9——相机标定
The NDK compiler so libraries
【工具配置篇】VSCode 常用使用总结
1、Citrix XenDesktop 2203之AD域系统安装(一)
八大排序之堆排序
DevExpress中针对指定列进行百分比转换
export使用
【LeetCode】235.二叉搜索树的最近公共祖先
(4) Rotating object detection data roLabelImg to DOTA format
Flink学习12:DataStreaming API
2022最强版应届生软件测试面试攻略
蓝牙gap协议
武田公司2022财年第一季度业绩强劲;正稳步实现全年的管理层指引目标
MySQL的主从模式搭建
Hong Kong International Jewellery Show and Hong Kong International Diamond, Gem and Pearl Show kick off
Using printf function in STM32
typescript64-映射类型
【MyCat简单介绍】









