Java 目标c中与BeanUtils.copyProperties等效。
我想知道它们是否在JAVA的方法“BeanUtils.CopyProperties(bean1,Bean2);”的目标C中有一个等价物 或其他解决方案,我想将motherObject强制转换为childObject:Java 目标c中与BeanUtils.copyProperties等效。,java,objective-c,inheritance,Java,Objective C,Inheritance,我想知道它们是否在JAVA的方法“BeanUtils.CopyProperties(bean1,Bean2);”的目标C中有一个等价物 或其他解决方案,我想将motherObject强制转换为childObject: @interface motherBean : NSObject{ ...} @interface childBean : motherBean { ...} motherBean m = [motherBean new]; childBean f = m; 在第一次测试中,它
@interface motherBean : NSObject{ ...}
@interface childBean : motherBean { ...}
motherBean m = [motherBean new];
childBean f = m;
在第一次测试中,它是有效的,但我有一个警告:“不兼容的指针类型正在返回…”
我使用WSDL2Objc并生成bean,它的名称可以在两代之间更改:-/ 我更喜欢和孩子一起工作,只是在她的定义中改变名字 谢谢
安东尼看看commons beanutils套餐。它有很多属性方法供你复制东西。特别是:
PropertyUtils.copyProperties(bean1, bean2);
但是,关于第二个问题,您试图将父类的实例向下转换为子类
我不确定这在任何OO语言中是如何合法的。确定您可以强制施放:
// This is not legal because you can't case from one class to anther
// unless the actual instance type (not the declared type of the variable,
// but the constructed type) is either the casted class or a subclass.
Parent p = new Parent();
Child c = (Child) p;
但是您会得到一个ClassCastException,因为您不能将父类的实例视为子类(只有相反的方式)。但是,以下任何一项都是合法的:
// This is legal because you're upcasting, which is fine
Child c = new Child();
Parent p = c;
// This is legal because the instance "p" is actually an
// instance of the "Child" class, so the downcast is legal.
Parent p = new Child();
Child c = (Child) p;
要回答第一个问题,可以轻松编写代码在实例之间复制属性值。如果您将属性限制为适当的Objective-C属性(使用@property()声明的项),这是最简单的,这可能是最好的做法。您可以使用Objective-C运行时函数获取类(class_getPropertyList)上所有属性的列表,并调用property_getName()获取属性的名称,并调用property_getAttributes()确保其可写。然后,可以使用NSObject的键值编码分别使用valueForKeyPath:和setValueForKeyPath:获取和设置属性值
代码示例的一些问题是实例应该是指针。其次,您需要显式强制转换,因为您要将类的实例分配给它的超类。反面不需要石膏。这可能就是您收到警告的原因。方法BeanUtils.copyProperties
//.h
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface BeanUtils : NSObject
+(void)copyProperties:(id)src dest:(id)dest;
@end
谢谢你的回答,这就是我寻找BeanUtils.CopyProperties(bean1,Bean2)等价物的原因;(或者PropertyUtils.copyProperties(bean1,bean2)的想法是一样的),但在Objective c中,而不是在JavaI中,我希望该方法已经存在。我写了一篇,然后贴在这里
//.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, @"");