相当于BeanUtils.copyProperties的Objective c。 [英] Equivalent in Objective c of BeanUtils.copyProperties.
本文介绍了相当于BeanUtils.copyProperties的Objective c。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道他们是否在JAVA的方法C中有一个等效的方法BeanUtils.CopyProperties(bean1,Bean2); ?
I would like to know if they have an equivalent in Objective C of the JAVA's methode "BeanUtils.CopyProperties(bean1, Bean2);" ?
或者其他解决方案,我想将motherObject转换为childObject:
Or other solution, i would like to cast motherObject to childObject :
@interface motherBean : NSObject{ ...}
@interface childBean : motherBean { ...}
motherBean m = [motherBean new];
childBean f = m;
第一次测试它的工作但是我有一个警告:不兼容的指针类型返回...... ;
With the first tests it's work but I have a warning : "incompatible pointer types returning ...";
我使用WSDL2Objc并生成bean,它的名称可以在2代之间改变: - /
I use WSDL2Objc and it generate bean, and the name of it can change between 2 generation :-/
谢谢
Anthony
推荐答案
方法BeanUtils.copyProperties
The method BeanUtils.copyProperties
//.h
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface BeanUtils : NSObject
+(void)copyProperties:(id)src dest:(id)dest;
@end
//.m
#import "BeanUtils.h"
@implementation BeanUtils
+(void)copyProperties:(id)src dest:(id)dest {
NSLog(@"classeSrc=%@ dst=%@", [src class], [dest class]);
if(src == NULL || dest == NULL) {
return;
}
Class clazz = [src class];
u_int count;
objc_property_t* properties = class_copyPropertyList(clazz, &count);
NSMutableArray* propertyArray = [NSMutableArray arrayWithCapacity:count];
for (int i = 0; i < count ; i++)
{
NSString *propertyName = [NSString stringWithUTF8String:property_getName(properties[i])];
[propertyArray addObject:propertyName];
//on verifie que la prop existe dans la classe dest
objc_property_t prop = class_getProperty([dest class], [propertyName UTF8String]);
if(prop != NULL) {
id result = [src valueForKey:propertyName];
[dest setValue:result forKey: propertyName];
}
else {
[NSException raise:NSInternalInconsistencyException format:@"La propriété %@ n'existe pas dans la classe %@",propertyName, [dest class] ];
}
}
free(properties);
}
@end
致电:
call :
EleveBean *eleveBean = [EleveBean new];
eleveBean.nom = @"bob";
eleveBean.prenom = @"john";
tns1_EleveBean *tnsEleve = [tns1_EleveBean new];
[BeanUtils copyProperties:eleveBean dest:tnsEleve];
STAssertEquals(eleveBean.nom, tnsEleve.nom, @"");
STAssertEquals(eleveBean.prenom, tnsEleve.prenom, @"");
这篇关于相当于BeanUtils.copyProperties的Objective c。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文