korgx9 korgx9 - 1 year ago 111
iOS Question

DCKeyValueObjectMapping JSON to Object mapping error

In Objective C for Object mapping I am using library DCKeyValueObjectMapping

Here is my class

#import <Foundation/Foundation.h>

@interface City : NSObject
@property(nonatomic, strong) NSString *id;
@property(nonatomic, strong) NSString *title;
@property(nonatomic, strong) NSString *slug;
@property(nonatomic) int country;
@property(nonatomic) Boolean isMain;
@property(nonatomic, strong) NSString *description;
@property(nonatomic, strong) NSString *position;
@property(nonatomic) Boolean isActive;
@property(nonatomic) Boolean isVisible;

Here is how I parse response

- (void)getCities {
NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"%@city/", APIURL]];

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:URL.absoluteString
success:^(NSURLSessionTask *task, id responseObject) {
DCParserConfiguration *config = [DCParserConfiguration configuration];
DCKeyValueObjectMapping *parser = [DCKeyValueObjectMapping mapperForClass: [City class] andConfiguration: config];
NSArray *cities = [parser parseArray: responseObject];
}failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error.localizedDescription);

Here is response from server:

country = 1;
description = 321;
id = 1;
"is_active" = 1;
"is_main" = 0;
"is_visible" = 1;
position = "POINT (42.8760663999999991 74.5912887000000069)";
slug = blabla;
title = "\U0411\U0438\U0448\U043a\U0435\U043a";
country = 1;
description = 123;
id = 2;
"is_active" = 1;
"is_main" = 0;
"is_visible" = 1;
position = "POINT (40.5266999999999982 72.8031000000000006)";
slug = bloblo;
title = "\U041e\U0448";

App is crashing every time on object mapping when I call this method.

Here is the debug message:

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

It is crash on converting property description from JSON to object. What I am doing wrong here?

Answer Source

Finally i found the problem

The problem is, I have declared property "description" in my class, which conflicts with property of parent class. In this case where you declared property description Xcode will show an warning message.

To solve this problem, you need to add to your implementation file .m one line code:

@synthesize description;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download