相当于BeanUtils.copyProperties的Objective c。

时间:2012-06-04 15:52:40

标签: java objective-c inheritance

我想知道他们是否在JAVA的方法C的目标C中有一个等价的“BeanUtils.CopyProperties(bean1,Bean2);” ?

或者其他解决方案,我想将motherObject转换为childObject:

@interface motherBean : NSObject{ ...}
@interface childBean : motherBean { ...}

motherBean m = [motherBean new];
childBean f = m;

第一次测试它的工作但是我有一个警告:“不兼容的指针类型返回......”;


我使用WSDL2Objc并生成bean,它的名称可以在2代之间改变: - /

我更喜欢与孩子一起工作,只需在她的定义中更改名称

谢谢

安东尼

3 个答案:

答案 0 :(得分:0)

看看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;

答案 1 :(得分:0)

要回答您的第一个问题,您可以轻松编写代码以在实例之间复制属性值。如果将属性限制为适当的Objective-C属性(使用@property()声明的项目)这是最简单的,这可能是最佳实践。您可以使用Objective-C运行时函数获取类(class_getPropertyList)上所有属性的列表,并调用property_getName()以获取属性的名称,并调用property_getAttributes()以确保它是可写的。然后,您可以使用NSObject的键值编码分别使用valueForKeyPath:和setValueForKeyPath:来获取和设置属性值。

代码示例的一些问题是实例应该是指针。其次,由于您要将类的实例分配给其超类,因此需要显式转换。反过来不需要演员。这可能就是你收到警告的原因。

答案 2 :(得分:0)

方法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, @"");