dchohfi / KeyValueObjectMapping

Automatic KeyValue Object Mapping for Objective-C, parse JSON/plist/Dictionary automatically
http://dchohfi.com/
MIT License
601 stars 90 forks source link

synthesize to value confuses the parser #10

Closed matzew closed 11 years ago

matzew commented 11 years ago

Here is my little model:

@interface Employee : NSObject

@property (nonatomic, copy) NSString* name;
@property (nonatomic, copy) NSArray* tasks;

@end

@interface Task : NSObject

@property (nonatomic, copy) NSString* title;
@property (nonatomic, copy) NSString* desc;
@end

Now, when using the @synthesizelike

@synthesize tasks = _tasks;

the parser no longer works:

-(void) testCustomMap {

    // set up some "faked JSON parsed Dictionary"
    NSMutableDictionary* task1 = [NSMutableDictionary dictionary];
    [task1 setValue:@"Title1" forKey:@"title"];
    [task1 setValue:@"Description1" forKey:@"desc"];

    NSMutableDictionary* task2 = [NSMutableDictionary dictionary];
    [task2 setValue:@"Title2" forKey:@"title"];
    [task2 setValue:@"Description2" forKey:@"desc"];

    NSMutableDictionary* employee1 = [NSMutableDictionary dictionary];
    [employee1 setValue:@"Matthias" forKey:@"name"];
    [employee1 setValue:[NSArray arrayWithObjects:task1, task2, nil] forKey:@"tasks"];

    DCArrayMapping *mapper = [DCArrayMapping mapperForClassElements:[Task class] forAttribute:@"tasks" onClass:[Employee class]];

    DCParserConfiguration *config = [DCParserConfiguration configuration];
    [config addArrayMapper:mapper];

    DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass:[Employee class]  andConfiguration:config];

    Employee *matthias = [parser parseDictionary:employee1];

    STAssertEqualObjects(@"Matthias", matthias.name, @"");

    NSLog(@"\n\n\n==>%@\n\n", [[matthias.tasks objectAtIndex:0] class]);

}

The class here is __NSCFDictionary. When using the @synthesize with out applying a value to it, like

@synthesize tasks;

The class is Task

dchohfi commented 11 years ago

Hi Matzew, thank you for opening this issue. You're right and I'm going to fix it today :) Hopefully you're enjoying the library!

dchohfi commented 11 years ago

@matzew could you check it out to see if solved? Some tests are added and here everything went fine.

matzew commented 11 years ago

Looks good! Can you push a new release to cocoapods?

dchohfi commented 11 years ago

Doing it now :)

dchohfi commented 11 years ago

It's there :) https://github.com/CocoaPods/Specs/pull/762