0

给定以下数据结构:

<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,因此没有初始化允许我访问属性的核心数据对象。

4

1 回答 1

1

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

newObject.olpersondepartment = [record objectForKey:@"OLPersonDepartment"];
newObject.olpersonemail      = [record objectForKey:@"OLPersonEmail"];
// etc.
于 2013-04-22T14:57:52.947 回答