iOS开发之遍历Model类的属性并完善使用Runtime给Model类赋值..docx
文本预览下载声明
iOS开发之遍历Model类的属性并完善使用Runtime给Model类赋值一、获取Model的实体属性1.要想遍历Model类的属性,首先得通过Runtime来获取该Model类有哪些属性,输出Model的所有属性的值可不像遍历Dictionary和Array那样一个for循环搞定的,下面的方法是通过Runtime来获取Model类的属性字符串,并以数组的形式返回。代码如下:///通过运行时获取当前对象的所有属性的名称,以数组的形式返回?-?(NSArray?*)?allPropertyNames{?///存储所有的属性名称?NSMutableArray?*allNames?=?[[NSMutableArray?alloc]?init];??///存储属性的个数?unsigned?int?propertyCount?=?0;??///通过运行时获取当前类的属性?objc_property_t?*propertys?=?class_copyPropertyList([self?class],?propertyCount);??//把属性放到数组中?for?(int?i?=?0;?i??propertyCount;?i?++)?{?///取出第一个属性?objc_property_t?property?=?propertys[i];??const?char?*?propertyName?=?property_getName(property);??[allNames?addObject:[NSString?stringWithUTF8String:propertyName]];?}??///释放?free(propertys);??return?allNames;?}?2.获取到Model类的属性方法后需要把属性字符串生成get方法,我们可以执行get方法来获取Model属性的值,下方的方法是根据属性字符串来获取属性的getter方法,OC中属性的getter方法的名字和属性的名字是一致的,生成getter方法比较简单,具体代码如下:#pragma?mark?--?通过字符串来创建该字符串的Setter方法,并返回?-?(SEL)?creatGetterWithPropertyName:?(NSString?*)?propertyName{??//1.返回get方法:?oc中的get方法就是属性的本身?return?NSSelectorFromString(propertyName);?}?二、Get方法的执行接下来要做的是通过Runtime来执行Getter方法,这一块需要通过方法的签名来执行Getter方法。在OC的运行时中要执行的方法需要传入参数或者需要接收返回值时,需要通过方法的签名来调用方法。下面的代码就是创建方法的签名,然后通过签名来获取调用的对象,在下边的方中回调用上述两个方法在通过方法的签名来获取Model属性的值,具体代码如下:-?(void)?displayCurrentModleProperty{??//获取实体类的属性名?NSArray?*array?=?[self?allPropertyNames];??//拼接参数?NSMutableString?*resultString?=?[[NSMutableString?alloc]?init];??for?(int?i?=?0;?i??array.count;?i?++)?{??//获取get方法?SEL?getSel?=?[self?creatGetterWithPropertyName:array[i]];??if?([self?respondsToSelector:getSel])?{??//获得类和方法的签名?NSMethodSignature?*signature?=?[self?methodSignatureForSelector:getSel];??//从签名获得调用对象?NSInvocation?*invocation?=?[NSInvocation?invocationWithMethodSignature:signature];??//设置target?[invocation?setTarget:self];??//设置selector?[invocation?setSelector:getSel];??//接收返回的值?NSObject?*__unsafe_unretained?returnValue?=?nil;??//调用?[invocation?invoke];??//接收返回值?[invocation?getReturnValue:returnValue];??[resultString?appendFormat:@%@\n,
显示全部