新建NSObject Category
如#import "NSObject+Propertys.h"
1.获取对象的所有属性
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| - (NSArray *)getAllProperties { Class subclass = [self class]; if (subclass == [NSObject class]) { return nil; } unsigned int propertyCount; objc_property_t *properties = class_copyPropertyList([self class], &propertyCount); NSMutableArray *result = [NSMutableArray array]; for (int i = 0; i < propertyCount; i++) { objc_property_t property = properties[i]; const char *propertyName = property_getName(property); NSString *key = @(propertyName); [result addObject:key]; } free(properties); return result.count ? [result copy] : nil; }
|
2.获取对象的所有属性和属性值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| - (NSDictionary *)getAllPropertiesAndVaules {
NSMutableDictionary *propertyDic = [NSMutableDictionary dictionary]; unsigned int outCount; objc_property_t *property = class_copyPropertyList([self class], &outCount); for (int i = 0; i < outCount; i++) { objc_property_t property_t = property[i]; NSString *propertyName = [NSString stringWithCString:property_getName(property_t) encoding:NSUTF8StringEncoding]; id propertyValue = [self valueForKey:propertyName]; if (propertyValue && propertyValue != nil) { [propertyDic setObject:propertyValue forKey:propertyName]; } } free(property); return propertyDic; }
|
3.获取对象的所有方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| -(void)getAllMethods { unsigned int mothCout_f =0; Method* mothList_f = class_copyMethodList([self class],&mothCout_f); for(int i=0;i<mothCout_f;i++) { Method temp_f = mothList_f[i]; const char* name_s =sel_getName(method_getName(temp_f)); int arguments = method_getNumberOfArguments(temp_f); const char* encoding =method_getTypeEncoding(temp_f); NSLog(@"方法名:%@,参数个数:%d,编码方式:%@",[NSString stringWithUTF8String:name_s], arguments, [NSString stringWithUTF8String:encoding]); } free(mothList_f); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
| -(NSDictionary *)getAllPropertyNameAndAttributes{ u_int count; objc_property_t *properties = class_copyPropertyList([self class], &count); NSMutableArray *propertyNameArray = [NSMutableArray arrayWithCapacity:count]; NSMutableArray *propertyAttributesArray = [NSMutableArray arrayWithCapacity:count]; for (int i = 0; i<count; i++) { objc_property_t property = properties[i]; const char *propertyName =property_getName(property); const char *propertyAttributes = property_getAttributes(property); [propertyNameArray addObject: [NSString stringWithUTF8String: propertyName]]; [propertyAttributesArray addObject:[NSString stringWithUTF8String: propertyAttributes]]; } NSDictionary *propertiesDictionary = [NSDictionary dictionaryWithObjects:propertyAttributesArray forKeys:propertyNameArray]; free(properties); return propertiesDictionary; }
#pragma mark---- 以下内容适用于成员变量 ivars -(NSArray *)allMemberVariables { unsigned int count = 0; Ivar *ivars = class_copyIvarList([self class], &count); NSMutableArray *result = [NSMutableArray array]; for (NSUInteger i = 0; i < count; ++i) { Ivar variable = ivars[i]; const char *name = ivar_getName(variable); NSString *varName = [NSString stringWithUTF8String:name]; [result addObject:varName]; } free(ivars); return result.count ? [result copy] : nil; }
+ (NSArray *)protocols { unsigned int outCount; Protocol * const *protocols = class_copyProtocolList([self class], &outCount); NSMutableArray *result = [NSMutableArray array]; for (int i = 0; i < outCount; i++) { unsigned int adoptedCount; Protocol * const *adotedProtocols = protocol_copyProtocolList(protocols[i], &adoptedCount); NSString *protocolName = [NSString stringWithCString:protocol_getName(protocols[i]) encoding:NSUTF8StringEncoding]; NSMutableArray *adoptedProtocolNames = [NSMutableArray array]; for (int idx = 0; idx < adoptedCount; idx++) { [adoptedProtocolNames addObject:[NSString stringWithCString:protocol_getName(adotedProtocols[idx]) encoding:NSUTF8StringEncoding]]; } NSString *protocolDescription = protocolName; if (adoptedProtocolNames.count) { protocolDescription = [NSString stringWithFormat:@"%@ <%@>", protocolName, [adoptedProtocolNames componentsJoinedByString:@", "]]; } [result addObject:protocolDescription]; } return result.count ? [result copy] : nil; }
|