方法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
致电:
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, @'');解决方法
我想知道它们是否在JAVA的方法“ BeanUtils.CopyProperties(bean1,Bean2);”的目标C中具有等效项。?
或其他解决方案,我想将motherObject强制转换为childObject:
@interface motherBean : NSObject{ ...}@interface childBean : motherBean { ...}motherBean m = [motherBean new];childBean f = m;
在第一个测试中,它可以正常工作,但是我有一个警告:“不兼容的指针类型返回…”;
我使用WSDL2Objc并生成Bean,并且其名称可以在2代之间更改:-/
我更喜欢与孩子一起工作,只是在她的定义中更改名字谢谢
安东尼