OS X编程

Objective-C Runtime技术调研(四)

2017-04-25  本文已影响4人  tridonlee

一、特征

TridonLee在大家心目中应该有很多特征吧,下面我们通过代码来获取TridonLee的特征。

TLPeople.h文件

@interface TLPeople :NSObject

{

NSString*_occupation;

NSString*_nationlity;

}

@property(nonatomic,copy)NSString*name;

@property(nonatomic)NSUIntegerage;

- (NSDictionary*)allProperties;

- (NSDictionary*)allIvars;

- (NSDictionary*)allMethods;

@end

TLPeople.m文件

#if TARGET_IPHONE_SIMULATOR

#import <objc/objc-runtime.h>

#else

#import <objc/runtime.h>

#import <objc/message.h>

#endif

@implementationTLPeople

-(NSDictionary*)allProperties {

unsignedintcount =0;

//获取类的所有属性,如果没有属性count就为0

objc_property_t*properties =class_copyPropertyList([selfclass], &count);

NSMutableDictionary*resultDict = [@{}mutableCopy];

for(NSUIntegeri =0; i < count; i ++) {

//获取属性的名称和值

constchar*propertyName =property_getName(properties[i]);

NSString*name = [NSStringstringWithUTF8String:propertyName];

idpropertyValue = [selfvalueForKey:name];

if(propertyValue) {

resultDict[name] = propertyValue;

}else{

resultDict[name] =@"字典的key对应的value不能为nil哦!";

}

}

//这里properties是一个数组指针,我们需要使用free函数来释放内存。

free(properties);

returnresultDict;

}

- (NSDictionary*)allIvars

{

unsignedintcount =0;

NSMutableDictionary*resultDict = [@{}mutableCopy];

Ivar*ivars =class_copyIvarList([selfclass], &count);

for(NSUIntegeri =0; i < count; i ++) {

constchar*varName =ivar_getName(ivars[i]);

NSString*name = [NSStringstringWithUTF8String:varName];

idvarValue = [selfvalueForKey:name];

if(varValue) {

resultDict[name] = varValue;

}else{

resultDict[name] =@"字典的key对应的value不能为nil哦!";

}

}

free(ivars);

returnresultDict;

}

- (NSDictionary*)allMethods

{

unsignedintcount =0;

NSMutableDictionary*resultDict = [@{}mutableCopy];

//获取类的所有方法,如果没有方法count就为0

Method*methods =class_copyMethodList([selfclass], &count);

for(NSUIntegeri =0; i < count; i ++) {

//获取方法名称

SELmethodSEL =method_getName(methods[i]);

constchar*methodName =sel_getName(methodSEL);

NSString*name = [NSStringstringWithUTF8String:methodName];

//获取方法的参数列表

intarguments =method_getNumberOfArguments(methods[i]);

resultDict[name] =@(arguments-2);

}

free(methods);

returnresultDict;

}

@end

在main.m中运行以下代码

int main(intargc,constchar* argv[]) {

@autoreleasepool{

TLPeople*liTeacher = [[TLPeoplealloc]init];

liTeacher.name=@"TridonLee";

liTeacher.age=18;

[liTeachersetValue:@"Teacher"forKey:@"occupation"];

NSDictionary*propertyResultDic = [liTeacherallProperties];

for(NSString*propertyNameinpropertyResultDic.allKeys) {

NSLog(@"propertyName:%@, propertyValue:%@",propertyName, propertyResultDic[propertyName]);

}

NSDictionary*ivarResultDic = [liTeacherallIvars];

for(NSString*ivarNameinivarResultDic.allKeys) {

NSLog(@"ivarName:%@, ivarValue:%@",ivarName, ivarResultDic[ivarName]);

}

NSDictionary*methodResultDic = [liTeacherallMethods];

for(NSString*methodNameinmethodResultDic.allKeys) {

NSLog(@"methodName:%@, argumentsCount:%@", methodName, methodResultDic[methodName]);

}

}

return 0;

}

最后的输出结果如下:

是不是有点失望,我没有加一些特殊的技能,留给下文了。此实战内容是通过TridonLee的一些特征学习:如何获取对象所有的属性名称和属性值、获取对象所有成员变量名称和变量值、获取对象所有的方法名和方法参数数量。

Demo传送门-> 4.2特征篇Demo

上一篇 下一篇

猜你喜欢

热点阅读