OC笔记 - isMemberOfClass | isKindOfClass
2022/8/2 6:23:53
本文主要是介绍OC笔记 - isMemberOfClass | isKindOfClass,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
isMemberOfClass | isKindOfClass
1 - 猜想以下代码输出的结果
1 #import <Foundation/Foundation.h> 2 #import <objc/runtime.h> 3 @interface Person : NSObject 4 5 @end 6 @implementation Person 7 8 @end 9 10 int main(int argc, const char * argv[]) { 11 @autoreleasepool { 12 13 NSLog(@"%d", [[NSObject class] isKindOfClass:[NSObject class]]); 14 NSLog(@"%d", [[NSObject class] isMemberOfClass:[NSObject class]]); 15 NSLog(@"%d", [[Person class] isKindOfClass:[Person class]]); 16 NSLog(@"%d", [[Person class] isMemberOfClass:[Person class]]); 17 18 NSLog(@"========== 以上代码等价如下 ========="); 19 20 NSLog(@"%d", [NSObject isKindOfClass:[NSObject class]]); 21 NSLog(@"%d", [NSObject isMemberOfClass:[NSObject class]]); 22 NSLog(@"%d", [Person isKindOfClass:[Person class]]); 23 NSLog(@"%d", [Person isMemberOfClass:[Person class]]); 24 25 } 26 return 0; 27 }
日志信息
2 - isMemberOfClass 和 isKindOfClass 源代码
1 - (BOOL)isMemberOfClass:(Class)cls { 2 return [self class] == cls; 3 } 4 - (BOOL)isKindOfClass:(Class)cls { 5 for (Class tcls = [self class]; tcls; tcls = tcls->superclass) { 6 if (tcls == cls) return YES; 7 } 8 return NO; 9 } 10 11 12 + (BOOL)isMemberOfClass:(Class)cls { 13 return object_getClass((id)self) == cls; 14 } 15 16 17 + (BOOL)isKindOfClass:(Class)cls { 18 for (Class tcls = object_getClass((id)self); tcls; tcls = tcls->superclass) { 19 if (tcls == cls) return YES; 20 } 21 return NO; 22 }
结论:isMemberOfClass 左边对象(类对象)是否等于右边所传进的类对象,是相等的关系
isKindOfClass 左边对象(类对象)是否等于右边所传进的类对象或是其子对象,是相等或是其子对象的关系
3 - 代码实例
1 #import <Foundation/Foundation.h> 2 #import <objc/runtime.h> 3 @interface Person : NSObject 4 5 @end 6 @implementation Person 7 8 @end 9 10 int main(int argc, const char * argv[]) { 11 @autoreleasepool { 12 13 // ----------------------------类方法 14 NSLog(@"%d", [Person isMemberOfClass:object_getClass([Person class])]); // 1 15 16 // NSObject 17 NSLog(@"%d", [Person isKindOfClass:object_getClass([NSObject class])]); // 1 18 NSLog(@"%d", [Person isKindOfClass:[NSObject class]]);// 1 19 20 // ----------------------------实例方法 21 id person = [[Person alloc] init]; 22 23 NSLog(@"%d", [person isMemberOfClass:[Person class]]); // 1 24 NSLog(@"%d", [person isMemberOfClass:[NSObject class]]); // 0 25 NSLog(@"%d", [person isKindOfClass:[Person class]]); // 1 26 NSLog(@"%d", [person isKindOfClass:[NSObject class]]); // 1 27 28 } 29 return 0; 30 }
这篇关于OC笔记 - isMemberOfClass | isKindOfClass的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-09CMS内容管理系统是什么?如何选择适合你的平台?
- 2025-01-08CCPM如何缩短项目周期并降低风险?
- 2025-01-08Omnivore 替代品 Readeck 安装与使用教程
- 2025-01-07Cursor 收费太贵?3分钟教你接入超低价 DeepSeek-V3,代码质量逼近 Claude 3.5
- 2025-01-06PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”
- 2025-01-05Easysearch 可搜索快照功能,看这篇就够了
- 2025-01-04BOT+EPC模式在基础设施项目中的应用与优势
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南
- 2025-01-03图像文字理解,OCR、大模型还是多模态模型?PalliGema2在QLoRA技术上的微调与应用
- 2025-01-03混合搜索:用LanceDB实现语义和关键词结合的搜索技术(应用于实际项目)