【问题标题】:dynamic typecasting of managed object properties when doing setValuesForKeysWithDictionary执行 setValuesForKeysWithDictionary 时托管对象属性的动态类型转换
【发布时间】:2012-05-04 19:47:30
【问题描述】:

我有几个 NSManagedObject 类。我正在从解析为 NSDictionary 对象的服务器中提取一些 JSON 数据。当发生从 JSON 到 NSDictionary 的转换时,我的所有数据都被转换为 NSStrings。然后,当我将此字典映射到我的 managedObject 时,我得到了这个:

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

所以我的 managedobject 正在寻找一个 NSNumber,但它正在获取一个字符串并抛出一个异常

当我调用setValuesForKeysWithDictionary 时,有没有一种方法可以自动为它们要进入的托管对象正确转换值?

谢谢!

【问题讨论】:

    标签: objective-c cocoa-touch


    【解决方案1】:

    在保存核心数据的同时管理 JSON 属性的最佳方法是编写一个可以覆盖 setValuesForKeysWithDictionary 的通用函数,如下所示:

    @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
    

    更多详情请参考此链接:http://www.cimgf.com/2011/06/02/saving-json-to-core-data/

    【讨论】:

      【解决方案2】:

      如果你收到的 json 实际上有数字值并且它们被转换为字符串,你应该得到一个新的 json 解析器。我推荐 NXJson。否则不会发生任何神奇的铸造。

      如果 json 返回诸如 {"idexpert":"1"} 之类的字符串,那么您可以覆盖 setValuesForKeysWithDictionary 并执行类似以下代码的操作;

      
      -(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];
      }
      

      【讨论】:

        猜你喜欢
        • 2015-11-25
        • 1970-01-01
        • 1970-01-01
        • 2013-02-19
        • 2018-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多