【问题标题】:Equivalent in Objective c of BeanUtils.copyProperties.等效于 BeanUtils.copyProperties 的 Objective c。
【发布时间】:2012-06-04 15:52:39
【问题描述】:

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

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

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

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

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


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

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

谢谢

安东尼

【问题讨论】:

    标签: java objective-c inheritance


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答,这就是我寻找等效 BeanUtils.CopyProperties(bean1, Bean2); 的原因(或 PropertyUtils.copyProperties(bean1, bean2) 是一样的想法)但在 Objective c 中不是在 Java 中
    【解决方案2】:

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

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

    【讨论】:

    • 我希望该方法已经存在。我写一个在这里发帖
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多