如何将数据类型从Plist与核心数据匹配

时间:2013-04-22 14:42:54

标签: ios core-data nsmanagedobject

鉴于以下数据结构:

<dict>
<key>Attachments</key>
<dict>
    <key>__deferred</key>
    <dict>
        <key>uri</key>
            <string>http://myserver/mysite/_vti_bin/listdata.svc/OLPerson(2)/Attachments</string>
    </dict>
</dict>
<key>ContentType</key>
<string>OLPerson</string>
<key>ContentTypeID</key>
<string>0x0100EA6E4D97C50CDB4C969ADE34F3DDD6630008BBD0E63B1D0C438CE98422AB07F904</string>
<key>Created</key>
<string>/Date(1358159519000)/</string>
<key>CreatedBy</key>
<dict>
    <key>__deferred</key>
    <dict>
        <key>uri</key>
            <string>http://myserver/mysite/_vti_bin/listdata.svc/OLPerson(2)/CreatedBy</string>
    </dict>
</dict>
<key>CreatedById</key>
<integer>1</integer>
<key>Id</key>
<integer>2</integer>
<key>Modified</key>
<string>/Date(1365691793000)/</string>
<key>ModifiedBy</key>
<dict>
    <key>__deferred</key>
    <dict>
        <key>uri</key>
            <string>http://myserver/mysite/_vti_bin/listdata.svc/OLPerson(2)/ModifiedBy</string>
    </dict>
</dict>
<key>ModifiedById</key>
<integer>1</integer>
<key>OLPersonBUId</key>
<integer>1</integer>
<key>OLPersonDepartment</key>
<string>Some Text Value</string>
<key>OLPersonEmail</key>
<string>someone@somewhere.com</string>
<key>OLPersonEmployeeID</key>
<string>0000002222</string>
<key>OLPersonFirstname</key>
<string>Some Firstname</string>
<key>OLPersonOfficeId</key>
<integer>1</integer>
<key>OLPersonSurname</key>
<string>Some Surname</string>
<key>Owshiddenversion</key>
<integer>4</integer>
<key>Path</key>
<string>/mysite/Lists/OLPerson</string>
<key>Version</key>
<string>1.0</string>
<key>__metadata</key>
<dict>
    <key>etag</key>
    <string>W/"4"</string>
    <key>type</key>
    <string>Microsoft.SharePoint.DataService.OLPersonItem</string>
    <key>uri</key>
    <string>http://myserver/mysite/_vti_bin/listdata.svc/OLPerson(2)</string>
</dict>
</dict>

在我的核心数据模型中的以下实体中,您可以看到我在我的核心数据模型中的json数据中返回了更多字段。我只对将json中的匹配字段放入模型中的相应字段到核心数据感兴趣。例如OLPersonBUId值应该进入核心数据中的olpersonbu。

#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>


@interface OLPerson : NSManagedObject

@property (nonatomic, retain) NSString * id;
@property (nonatomic, retain) NSDate * olcreatedat;
@property (nonatomic, retain) NSString * olpersonbuid;
@property (nonatomic, retain) NSString * olpersondepartment;
@property (nonatomic, retain) NSString * olpersonemail;
@property (nonatomic, retain) NSString * olpersonemployeeid;
@property (nonatomic, retain) NSString * olpersonfirstname;
@property (nonatomic, retain) NSString * olpersonoffice;
@property (nonatomic, retain) NSData * olpersonphoto;
@property (nonatomic, retain) NSString * olpersonsurname;
@property (nonatomic, retain) NSNumber * olsyncstatus;
@property (nonatomic, retain) NSDate * olupdatedat;

@end

我遇到一个问题,即整数值不会进入我的核心数据模型,因为它需要一个字符串值。正确的数据类型在核心数据中定义,但并不总是在JSON中返回正确的数据类型。

我有以下代码:

- (void)newManagedObjectWithClassName:(NSString *)className forRecord:(NSDictionary *)record {
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:className inManagedObjectContext:[[OLCoreDataController sharedInstance] backgroundManagedObjectContext]];
[record enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
    [self setValue:obj forKey:key forManagedObject:newManagedObject];
}];
[record setValue:[NSNumber numberWithInt:OLObjectSynced] forKey:@"olsyncstatus"];
}

但它失败了,因为它所期望的类型不是它从JSON数据中得到的类型。

如何查看上述数据中值的数据类型并将该数据放入Core Data中,同时只输入匹配的键值并确保进入值的数据具有正确的数据类型?

[编辑]

添加以下代码以进一步解释此问题。我现在遇到的问题是我无法获得NSDictionary填充属性的属性:

- (void)newManagedObjectWithClassName:(NSString *)className forRecord:(NSDictionary *)record {
NSManagedObject *newManagedObject = [NSEntityDescription insertNewObjectForEntityForName:className inManagedObjectContext:[[OLCoreDataController sharedInstance] backgroundManagedObjectContext]];

NSDictionary *attributes = [[????????] attributesByName];
    for (NSString *attribute in record) {
        id key = nil;
        id value = [key objectForKey:attribute];
        if (value == nil) {
            // Don't attempt to set nil, or you'll overwite values in self that aren't present in keyedValues
            continue;
        }
        NSAttributeType attributeType = [[record 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]];
        }
        [self setValue:value forKey:attribute forManagedObject:newManagedObject];
    }

}

我传入了className,因此没有初始化的核心数据对象允许我访问属性。

1 个答案:

答案 0 :(得分:1)

您必须按属性手动浏览字典。这是进行此导入的唯一安全方式。

newObject.olpersondepartment = [record objectForKey:@"OLPersonDepartment"];
newObject.olpersonemail      = [record objectForKey:@"OLPersonEmail"];
// etc.