iOS的runtime获取类的属性的数据类型
来源:互联网 发布:挚爱梵高 知乎 编辑:程序博客网 时间:2024/06/06 05:29
iOS的runtime获取类的属性的数据类型
#import "NSString+Property.h"#import <objc/runtime.h>#include <string.h>@implementation NSString (Property)/** * Get the data type of a property in a class * @designatedClass, designated class name of `Class` */- (NSString *)getPropertyTypeWithClass:(Class)designatedClass { const char * propertyName = self.UTF8String; NSString *property_data_type = nil; unsigned int outCount = 0, i = 0; objc_property_t *properties = class_copyPropertyList(designatedClass, &outCount); for (; i < outCount; i++) { objc_property_t property = properties[i]; const char *property_name = property_getName(property); if (strcmp(property_name, propertyName) == 0) { const char * property_attr = property_getAttributes(property); //If the property is a type of Objective-C class, then substring the variable of `property_attr` in order to getting its real type if (property_attr[1] == '@') { char * occurs1 = strchr(property_attr, '@'); char * occurs2 = strrchr(occurs1, '"'); char dest_str[40]= {0}; strncpy(dest_str, occurs1, occurs2 - occurs1); char * realType = (char *)malloc(sizeof(char) * 50); int i = 0, j = 0, len = (int)strlen(dest_str); for (; i < len; i++) { if ((dest_str[i] >= 'a' && dest_str[i] <= 'z') || (dest_str[i] >= 'A' && dest_str[i] <= 'Z')) { realType[j++] = dest_str[i]; } } property_data_type = [NSString stringWithFormat:@"%s", realType]; free(realType); } else { //Otherwise, take the second subscript character for comparing to the @encode() char * realType = [self getPropertyRealType:property_attr]; property_data_type = [NSString stringWithFormat:@"%s", realType]; } } } return property_data_type;}/** * Get the data type of a property in a class that the encode characters is deponding on the following Link * https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100-SW1 **/- (char *)getPropertyRealType:(const char *)property_attr { char * type; char t = property_attr[1]; if (strcmp(&t, @encode(char)) == 0) { type = "char"; } else if (strcmp(&t, @encode(int)) == 0) { type = "int"; } else if (strcmp(&t, @encode(short)) == 0) { type = "short"; } else if (strcmp(&t, @encode(long)) == 0) { type = "long"; } else if (strcmp(&t, @encode(long long)) == 0) { type = "long long"; } else if (strcmp(&t, @encode(unsigned char)) == 0) { type = "unsigned char"; } else if (strcmp(&t, @encode(unsigned int)) == 0) { type = "unsigned int"; } else if (strcmp(&t, @encode(unsigned short)) == 0) { type = "unsigned short"; } else if (strcmp(&t, @encode(unsigned long)) == 0) { type = "unsigned long"; } else if (strcmp(&t, @encode(unsigned long long)) == 0) { type = "unsigned long long"; } else if (strcmp(&t, @encode(float)) == 0) { type = "float"; } else if (strcmp(&t, @encode(double)) == 0) { type = "double"; } else if (strcmp(&t, @encode(_Bool)) == 0 || strcmp(&t, @encode(bool)) == 0) { type = "BOOL"; } else if (strcmp(&t, @encode(void)) == 0) { type = "void"; } else if (strcmp(&t, @encode(char *)) == 0) { type = "char *"; } else if (strcmp(&t, @encode(id)) == 0) { type = "id"; } else if (strcmp(&t, @encode(Class)) == 0) { type = "Class"; } else if (strcmp(&t, @encode(SEL)) == 0) { type = "SEL"; } else { type = ""; } return type;}@end
阅读全文
0 0
- iOS的runtime获取类的属性的数据类型
- iOS runtime 学习之类的属性动态获取(一)
- iOS runtime 根据类名获取属性的类型和名称
- OC 的 Runtime 动态获取对象属性
- Runtime获取对象隐藏的属性
- 通过runtime获取一个类的所有属性
- runtime--获取对应类的变量及属性
- ios 利用runtime打印类的所有属性
- ios 通过runtime 获取类对象的所有子类
- [iOS] ios的runtime
- [iOS] ios的runtime
- [iOS] ios的runtime
- Objective-C Runtime的数据类型
- iOS获取类的属性列表
- iOS获取屏幕的属性
- ios runtime 获取 class 属性
- iOS的runtime
- IOS runtime的运用
- KVO键值观察运用及其原理
- 在ssh项目中spring在action注入service为null
- ActiveList API
- 安卓目录地址getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()的作用
- 【jQuery源码浅析】(五)--文档加载--$.ready
- iOS的runtime获取类的属性的数据类型
- 翻滚的TextView
- myeclipse打开软件出现Building workspace’has encountered a problem.Error occurred during the build。错误
- 007_内核对象的Signal状态
- Activity的总结(一)
- 供SAPI中TTS功能用使的Win8.1语言包安装
- TokenSearchManager API
- 让APK只包含指定的ABI
- 查询数据库中带有某个字段的所有表名