Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/107.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c RestKit:如何将URL参数映射到对象属性_Objective C_Ios_Restkit - Fatal编程技术网

Objective c RestKit:如何将URL参数映射到对象属性

Objective c RestKit:如何将URL参数映射到对象属性,objective-c,ios,restkit,Objective C,Ios,Restkit,我有这样的休息服务方式 /GetOfficeDocument?officeId=259 它返回一个文档数组。应用程序中的文档是与办公室有关系的NSManagedObject对象。如何将我的文档的关系映射到officeId参数到office 我知道我应该重写objectLoader:willMapData:,但我不知道在这个方法内部应该做什么。这些文件是无用的 UPD。服务器的响应如下所示: [{"AddedDate":"\/Date(1261484400000+0400)\/","Title"

我有这样的休息服务方式

/GetOfficeDocument?officeId=259
它返回一个文档数组。应用程序中的文档是与办公室有关系的NSManagedObject对象。如何将我的
文档的
关系映射到
officeId
参数到
office

我知道我应该重写
objectLoader:willMapData:
,但我不知道在这个方法内部应该做什么。这些文件是无用的

UPD。服务器的响应如下所示:

[{"AddedDate":"\/Date(1261484400000+0400)\/","Title":"Some text","Uri":"\/Document\/News\/851"}]
正如您所看到的,
officeId
不包含在响应中,只包含在URL中。我可以在
objectLoader:willMapData:
中使用

[[[loader URL] queryParameters] objectForKey:@"officeId"]

但是我接下来应该把它放在哪里呢?可映射数据参数是一个可变数组,我应该在那里放置什么?不知道。

我通常会将
RKObjectMapping
添加到managedObject类中

将此添加到您的文档中。h

  + (RKObjectMapping *)objectMapping;
将此方法添加到文档中。m

+ (RKObjectMapping *)objectMapping {
RKManagedObjectMapping *mapping = [RKManagedObjectMapping mappingForClass:[self class] inManagedObjectStore:[[RKObjectManager sharedManager] objectStore]];
     mapping.primaryKeyAttribute = @"word";
    [mapping mapKeyPath:@"word" toAttribute:@"word"];
    [mapping mapKeyPath:@"min_lesson" toAttribute:@"minLesson"];
}

当然,您应该更改文档对象属性的键路径。每一对都是服务器响应上的密钥的名称,也是managedObject上对应的密钥路径

然后,在初始化
objectManager
时,可以为您拥有的每个managedObject设置映射

 RKManagedObjectStore *store = [RKManagedObjectStore objectStoreWithStoreFilename:databaseName usingSeedDatabaseName:seedDatabaseName managedObjectModel:nil delegate:self];
objectManager.objectStore = store;

 //set the mapping object from your Document class
[objectManager.mappingProvider setMapping:[SRLetter objectMapping] forKeyPath:@"Document"];

你可以在这里找到一个很棒的教程-。在文章的中间,你会找到有关映射的数据。

你可以尝试在响应中返回的每个文档项中输入<代码> Office ID <代码>值,如:

- (void)objectLoader:(RKObjectLoader *)loader willMapData:(inout __autoreleasing id *)mappableData
{
    NSString *officeId = [[[loader URL] queryParameters] objectForKey:@"officeId"];

    NSMutableArray *newMappableData = [[NSMutableArray alloc] initWithCapacity:[*mappableData count]];

    for (NSDictionary *documentDict in *mappableData)
    {
        NSMutableDictionary = newDocumentDict = [documentDict mutableCopy];
        [newDocumentDict setObject:officeId forKey:@"OfficeId"];
        [newMappableData addObject:newDocumentDict];
    }

    *mappableData = newMappableData;
}
并在
文档中使用类似于以下内容的映射:

[documentMapping mapAttributes:@"AddedDate", @"Title", @"Uri", @"OfficeId", nil];
[documentMapping mapKeyPath:@"" toRelationship:@"office" withMapping:officeMapping];
[documentMapping connectRelationship:@"office" withObjectForPrimaryKeyAttribute:@"OfficeId"];

您可以给出web服务返回的响应的示例吗?
[{“AddedDate”:“\/Date(1261484400000+0400)\/”,“Title”:“Some text”,“Uri”:“\/Document\/News\/851”}]
-正如您所看到的,
officeId
不包含在响应中,而只包含在URL中。我可以在
objectLoader:willMapData:
中使用
[[[loader URL]queryParameters]objectForKey:@“officeId”]
提取它,但是我接下来应该把它放在哪里呢?据我所知,您使用的是旧版本的RestKit,教程已经过时了。哦,终于有了一些有用的东西。至少我现在知道如何操作mappableData了。但是,第二部分不起作用——这里已经讨论过了: