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 に答える