0

I have a few NSManagedObject classes. I am pulling down some JSON data from a server which I parse into an NSDictionary object. When the conversion from JSON to NSDictionary occurs, all of my data is cast as NSStrings. When I then map this dictionary to my managedObject I get this:

Unacceptable type of value for attribute: property = "idexpert"; desired type = NSNumber; given type = __NSCFString; value = 1.'

So my managedobject is looking for an NSNumber but it's getting a string and throwing an exception

Is there a way that when I call setValuesForKeysWithDictionary I can automagically cast the values properly for the managedobject they are going into?

Thanks!

2 Answers 2

1

The best way to manage JSON attributes while saving in core data is to write a generic function that can override setValuesForKeysWithDictionary as per below:

@implementation NSManagedObject (safeSetValuesKeysWithDictionary)

- (void)safeSetValuesForKeysWithDictionary:(NSDictionary *)keyedValues dateFormatter:(NSDateFormatter *)dateFormatter
{
    NSDictionary *attributes = [[self entity] attributesByName];
    for (NSString *attribute in attributes) {
        id value = [keyedValues objectForKey:attribute];
        if (value == nil) {
            continue;
        }
        NSAttributeType attributeType = [[attributes objectForKey:attribute] attributeType];
        if ((attributeType == NSStringAttributeType) && ([value isKindOfClass:[NSNumber class]])) {
            value = [value stringValue];
        } else if (((attributeType == NSInteger16AttributeType) || (attributeType == NSInteger32AttributeType) || (attributeType == NSInteger64AttributeType) || (attributeType == NSBooleanAttributeType)) && ([value isKindOfClass:[NSString class]])) {
            value = [NSNumber numberWithInteger:[value integerValue]];
        } else if ((attributeType == NSFloatAttributeType) &&  ([value isKindOfClass:[NSString class]])) {
            value = [NSNumber numberWithDouble:[value doubleValue]];
        } else if ((attributeType == NSDateAttributeType) && ([value isKindOfClass:[NSString class]]) && (dateFormatter != nil)) {
            value = [dateFormatter dateFromString:value];
        }
        [self setValue:value forKey:attribute];
    }
}
@end

For more details refer this link here: http://www.cimgf.com/2011/06/02/saving-json-to-core-data/

0

If the json you are receiving actually has number values and they are getting cast as string you should get a new json parser. I recommend NXJson. Otherwise there wont be any magical casting happening.

If the json is returning strings such as {"idexpert":"1"} then you can override setValuesForKeysWithDictionary and do something like the code below;


-(void)setValuesForKeysWithDictionary:(NSDictionary *)d{
   NSMutableDictionary *newDict = [NSMutableDictionary dictionaryWithDictionary:d];
   NSString *value = [newDict valueForKey:@"idexpert"];
   [newDict setValue:[NSNumber numberWithLong:[value longValue]] forKey:@"idexpert"];
   [super setValuesForKeysWithDictionary:newDict];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.