使用反射的属性类型或类

编程入门 行业动态 更新时间:2024-10-25 10:23:49
本文介绍了使用反射的属性类型或类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道是否有可能确定Objects属性的类或原始类型.获取所有属性名称和值非常容易. SO答案

I was wondering if it's possible to determine the class or primitive type of an Objects properties. Getting all properties names and values is pretty easy. SO answer

那么当属性没有值或没有nil值时,有什么方法可以获取属性类类型吗?

So is there any way to get the properties class type while the property hast no value or nil value?

示例代码

@interface MyObject : NSObject @property (nonatomic, copy) NSString *aString; @property (nonatomic, copy) NSDate *aDate; @property NSInteger aPrimitive; @end @implementation MyObject @synthesize aString; @synthesize aDate; @synthesize aPrimitive; - (void)getTheTypesOfMyProperties { unsigned int count; objc_property_t* props = class_copyPropertyList([self class], &count); for (int i = 0; i < count; i++) { objc_property_t property = props[i]; // Here I can easy get the name or value const char * name = property_getName(property); // But is there any magic function that can tell me the type? // the property can be nil at this time Class cls = magicFunction(property); } free(props); } @end

推荐答案

在有关objc运行时的苹果文档,并根据这样的答案我终于使它起作用了.我只想分享我的结果.

After searching through Apples Documentation about objc Runtime and according to this SO answer I finally got it working. I just want to share my results.

unsigned int count; objc_property_t* props = class_copyPropertyList([MyObject class], &count); for (int i = 0; i < count; i++) { objc_property_t property = props[i]; const char * name = property_getName(property); NSString *propertyName = [NSString stringWithCString:name encoding:NSUTF8StringEncoding]; const char * type = property_getAttributes(property); NSString *attr = [NSString stringWithCString:type encoding:NSUTF8StringEncoding]; NSString * typeString = [NSString stringWithUTF8String:type]; NSArray * attributes = [typeString componentsSeparatedByString:@","]; NSString * typeAttribute = [attributes objectAtIndex:0]; NSString * propertyType = [typeAttribute substringFromIndex:1]; const char * rawPropertyType = [propertyType UTF8String]; if (strcmp(rawPropertyType, @encode(float)) == 0) { //it's a float } else if (strcmp(rawPropertyType, @encode(int)) == 0) { //it's an int } else if (strcmp(rawPropertyType, @encode(id)) == 0) { //it's some sort of object } else { // According to Apples Documentation you can determine the corresponding encoding values } if ([typeAttribute hasPrefix:@"T@"]) { NSString * typeClassName = [typeAttribute substringWithRange:NSMakeRange(3, [typeAttribute length]-4)]; //turns @"NSDate" into NSDate Class typeClass = NSClassFromString(typeClassName); if (typeClass != nil) { // Here is the corresponding class even for nil values } } } free(props);

更多推荐

使用反射的属性类型或类

本文发布于:2023-11-12 11:44:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581410.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反射   属性   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!