Вопрос по objective-c, java, inheritance – Эквивалент в Objective c из BeanUtils.copyProperties.

1

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

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

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

Error: User Rate Limit Exceeded


Error: User Rate Limit Exceeded

I prefere to work with the child and just change the name in her definition

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Ваш Ответ

3   ответа
0

Error: User Rate Limit Exceeded

PropertyUtils.copyProperties(bean1, bean2);

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded

// 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;
0

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded

Error: User Rate Limit Exceeded Anthone
0

Error: User Rate Limit Exceeded

//.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

Error: User Rate Limit Exceeded

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

Похожие вопросы