1

Java のメソッド「BeanUtils.CopyProperties(bean1, Bean2);」の Objective C に同等のものがあるかどうかを知りたいです。?

または他の解決策として、 MotherObject を childObject にキャストしたいと思います:

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

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

最初のテストでは動作しますが、「互換性のないポインター型が返されます...」という警告があります。


私は WSDL2Objc を使用し、Bean を生成します。その名前は 2 世代間で変更できます :-/

私は子供と一緒に働き、彼女の定義の名前を変更することを好みます

ありがとう

アンソニー

4

3 に答える 3

0

最初の質問に答えるために、インスタンス間でプロパティ値をコピーするコードを簡単に記述できます。プロパティを適切なObjective-Cプロパティ(@property()を使用して宣言されたアイテム)に制限するのが最も簡単です。これは、とにかくおそらくベストプラクティスです。Objective-Cランタイム関数を使用してクラス(class_getPropertyList)のすべてのプロパティのリストを取得し、property_getName()を呼び出してプロパティの名前を取得し、property_getAttributes()を呼び出して書き込み可能であることを確認できます。次に、NSObjectのKey Value Codingを使用して、valueForKeyPath:とsetValueForKeyPath:をそれぞれ使用してプロパティ値を取得および設定できます。

コード例に関するいくつかの問題は、インスタンスがポインタである必要があることです。次に、クラスのインスタンスをそのスーパークラスに割り当てるため、明示的なキャストが必要です。逆はキャストを必要としません。おそらくそれが警告を受け取っている理由です。

于 2012-06-04T18:14:23.130 に答える
0

commons-beanutilsパッケージを見てください。それはあなたがものをコピーするためのたくさんのプロパティメソッドを持っています。特に:

PropertyUtils.copyProperties(bean1, bean2);

ただし、2番目の質問に関しては、親クラスのインスタンスを子クラスにダウンキャストしようとしていますか?

それがどのオブジェクト指向言語でもどのように合法であるかはわかりません。確かにあなたは強制的にキャストすることができます:

// 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;
于 2012-06-04T18:08:55.603 に答える
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, @"");
于 2012-06-05T10:41:21.983 に答える