aryaxt / OCMapper

Objective-C library to easily map NSDictionary to model objects, works perfectly with Alamofire. ObjectMapper works similar to GSON
MIT License
347 stars 45 forks source link

Crash with : class is not key value coding-compliant for the key(s): -hash or -debugDescription or -description #39

Open greinaldor opened 9 years ago

greinaldor commented 9 years ago

I use an instance of ObjectMapper and then configure it by adding an InCodeMappingProvider as the mappingProvider. For example :

self.mapper_ = [[ObjectMapper alloc] init];
InCodeMappingProvider *inCodeProvider = [[InCodeMappingProvider alloc] init];
[self.mapper setMappingProvider:inCodeProvider];

I have a user object like this :

@interface SSUserStats : SSObject <SSObjectMapping>

@property (nonatomic, strong, nullable) NSNumber * storeCount;
@property (nonatomic, strong, nullable) NSNumber * followerCount;
@property (nonatomic, strong, nullable) NSNumber * followeeCount;

@end

@interface SSUser : NSObject <ObjectMapping>

@property (nonatomic, strong, nullable) NSString  *userId;
@property (nonatomic, strong, nullable) NSString  *email;
@property (nonatomic, strong, nullable) NSString  *username;
@property (nonatomic, strong, nullable) NSString  *firstName;

@property (nonatomic, strong, nullable) UserStats *stats;

@property (nonatomic, strong, nullable) NSString  *profileImageFilename;
@property (nonatomic, strong, nullable) NSString  *headerImageFilename;

@end

When then I try to create a dictionary from a user object and then convert back this dictionary to an object my app crash :

SSUserStats *stats = [[SSUserStats alloc] init];
stats.storeCount = @(42);
stats.followeeCount = @(12);
stats.followerCount = @(21);

SSUser  *user = [[SSUser alloc] init];
user.firstName = @"Adrien";
user.email = @"toto@toto.com";
user.username = @"flop";
user.stats = stats;

NSDictionary *dict = [self.mapper dictionaryFromObject:user];
NSLog(@"Dic : %@", dict);
User *user2 = [self.mapper objectFromSource:dict toInstanceOfClass:[User class]];
NSLog(@"User2 : %@", [user2 description]);

First log shows :

Dic : {
    debugDescription = "<User: 0x7fa183e0cd20>";
    description = "<User: 0x7fa183e0cd20>";
    firstName = Adrien;
    email = @"toto@toto.com";;
    hash = 140331679010080;
    stats =     {
        debugDescription = "<UserStats: 0x7fa183e2dde0>";
        description = "<UserStats: 0x7fa183e2dde0>";
        followeeCount = 12;
        followerCount = 21;
        hash = 140331679145440;
        storeCount = 42;
        superclass =         {
        };
    };
    superclass = NSObject;
}

At the third line, my program crash with :

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<User 0x7fa183f22360> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key hash.'

I'm using Xcode 7, iOS 9. It's really sad because I really like this lib and the way it's architectured. My question is why : hash / superclass / debugDescription / description are processed which leads to this crash, Any idea of how I can fix this. Many thanks.

aryaxt commented 9 years ago

I'm updating my code to swift 2.0 and Xcode 7 this weekend, will fix it and release a new version before Monday if the fix is simple

greinaldor commented 9 years ago

Wow nice, this would be really good ;) Keep going the good work.

aryaxt commented 9 years ago

I'm having a problem reproducing this issue. can you create a failing unit test using the models I have in the project?