获取Model的实体属性
- (NSArray*)allPropertyNames
{
NSMutableArray*allNames = [[NSMutableArray alloc]init];
unsigned int propertyCount =0;
objc_property_t *propertys =class_copyPropertyList([selfclass], &propertyCount);
for(inti =0; i < propertyCount; i ++)
{
objc_property_tproperty = propertys[i];
constchar* propertyName =property_getName(property);
[allNames addObject:[NSStringstringWithUTF8String:propertyName]];
}
free(propertys);
return allNames;
}
实现Get和Set方法
//创建Getter方法
- (SEL)createGetterWithPropertyName:(NSString*)propertyName
{
return NSSelectorFromString(propertyName);
}
//创建Setter方法
- (SEL)createSetterWithPropertyName:(NSString*)propertyName
{
if(!propertyName)
{
return nil;
}
NSString*firstStr = [propertyName substringToIndex:1];
NSString*upperFirstStr = firstStr.uppercaseString;
NSMutableString*mutablePropertyName = [[NSMutableString alloc]initWithString:propertyName];
[mutablePropertyName replaceCharactersInRange:NSMakeRange(0,1)withString:upperFirstStr];
[mutablePropertyName insertString:@"set"atIndex:0];
[mutablePropertyName appendString:@":"];
return NSSelectorFromString(mutablePropertyName);
}
执行Get和Set
//执行get方法
- (void)displayCurrentModleProperty
{
NSArray*propertyArray = [self allPropertyNames];
NSMutableString*resultString = [NSMutableString new];
for(inti =0; i < propertyArray.count; i ++)
{
SELgetSel = [selfcreateGetterWithPropertyName:propertyArray[i]];
if([self respondsToSelector:getSel])
{
NSMethodSignature*signature = [self methodSignatureForSelector:getSel];
NSInvocation*invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self];
[invocation setSelector:getSel];
NSObject*returnValue =nil;
[invocation invoke];
[invocation getReturnValue:&returnValue];
[resultString appendFormat:@"%@\n",returnValue];
}
}
NSLog(@"%@",resultString);
}
//执行setter方法
- (void)setCurrentModelValueWithPropertyStr:(NSString*)propertyStr withValue:(NSString*)strValue;
{
SEL setSel = [self createSetterWithPropertyName:propertyStr];
NSMethodSignature*signature = [self methodSignatureForSelector:setSel];
NSInvocation*invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setTarget:self];
[invocation setSelector:setSel];
[invocation setArgument:&strValueatIndex:2];
[invocation invoke];
}
Dictionary的Key与Model的属性不同的处理方式
//映射 及对映射的处理
- (NSDictionary*)propertyMapDic
{
return@{
@"name":@"name",
@"value":@"age",
};
}
- (void)assignToPropertyWithNoMapDictionary:(NSDictionary*)data
{
NSDictionary*propertyMapDic = [self propertyMapDic];
NSArray *dicKeyArray = [data allKeys];
NSMutableDictionary *tempDic = [NSMutableDictionary new];
for(inti =0; i < dicKeyArray.count; i ++)
{
NSString*key = dicKeyArray[i];
[tempDic setObject:data[key]forKey:propertyMapDic[key]];
}
[self assginToPropertyWithDictonary:tempDic];
}
- (void)assginToPropertyWithDictonary:(NSDictionary*)dic
{
NSArray*propertyArray = [self allPropertyNames];
[propertyArray enumerateObjectsUsingBlock:^(id_Nonnullobj,NSUIntegeridx,BOOL*_Nonnullstop) {
NSString*propertyStr = (NSString*)obj;
NSString*valueStr = [dic objectForKey:propertyStr];
[self setCurrentModelValueWithPropertyStr:propertyStrwithValue:valueStr];
}];
[self displayCurrentModleProperty];
}