当前位置:网站首页>【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.
边栏推荐
猜你喜欢
随机推荐
《PyTorch深度学习实践》第十一课(卷积神经网络CNN高级版)
Mysql master-slave delay reasons and solutions
浮点数基础知识
After the firewall iptable rule is enabled, the system network becomes slow
Flink学习11:flink程序并行度
Pytorch distributed parallel processing
香港国际珠宝展及香港国际钻石、宝石及珍珠展揭幕
矩阵的构造
微信小程序仿input组件、虚拟键盘
技术分析模式(八)双顶和底
专用机终端安装软件后报IP冲突
typescript60-泛型工具类型(readonly)
【C语言】结构体变量数据通过 void* 传入到函数中
Takeda Fiscal 2022 First Quarter Results Strong; On Track to Achieve Full-Year Management Guidance
概率与期望部分题解
Falsely bamboo brother today and found a localization of API to use tools
Database table insert data
RK3568环境安装
IO process thread -> communication between processes -> day7
【内推】新相微电子









