2013-09-16 224 views
1

如何在映射實體時設置靜態值?在restkit映射中設置靜態值

我有這樣的JSON響應:

"friends": [ 
    { 
     "id": 123, 
     "name": "Friend", 
    }, 
] 
"featured": [ 
    { 
     "id": 456, 
     "name": "Some Featured user", 
    }, 
] 

我的映射和描述是這樣的:

RKMapping *friendsMapping = [ProjectMappingProvider userMapping]; 
RKMapping *featuredMapping = [ProjectMappingProvider featuredUserMapping]; 

RKResponseDescriptor *friendsResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:friendsMapping 
     method:RKRequestMethodGET 
     pathPattern:@"/api/users" 
     keyPath:@"friends" 
     statusCodes:statusCodeSet]; 

RKResponseDescriptor *featuredResponseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:friendsMapping 
     method:RKRequestMethodGET 
     pathPattern:@"/api/users" 
     keyPath:@"featured" 
     statusCodes:statusCodeSet]; 

RKManagedObjectRequestOperation *operation = [[RKManagedObjectRequestOperation alloc] initWithRequest:request 
     responseDescriptors:@[ 
     friendsResponseDescriptor, 
     featuredResponseDescriptor]]; 

... some code emited for readabilty ... 

現在畝friendsResponseDescriptor和featuredResponseDescriptors外觀幾乎一樣,但我想設置附加CoreData參數。通過friendsDescriptor映射的對象應該有section = 0,通過特徵描述符映射的對象應該有section = 10。

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"User" 
               inManagedObjectStore:[[DataModel sharedDataModel] objectStore]]; 
[mapping addAttributeMappingsFromDictionary:@{ 
               @"id": @"userId", 
               @"name": @"name"             }]; 
mapping.identificationAttributes = @[ @"userId" ]; 

// How can I do somethning like this? 
[mapping setValue:@0 forKey:@"section"]; 

而且功能映射:

RKEntityMapping *mapping = [RKEntityMapping mappingForEntityForName:@"User" 
               inManagedObjectStore:[[DataModel sharedDataModel] objectStore]]; 
[mapping addAttributeMappingsFromDictionary:@{ 
               @"id": @"userId", 
               @"name": @"name"             }]; 
mapping.identificationAttributes = @[ @"userId" ]; 

// How can I do somethning like this? 
[mapping setValue:@10 forKey:@"section"]; 

請注意,我沒有whetever用戶的任何其它指標是朋友或用戶JSON自身特色。我可以區分用戶類型(朋友,精選)的唯一方法是在用戶設置的JSON響應列表中。 我稍後在表格視圖控制器中使用section屬性來創建節。

回答

0

如果您使用不同的實體,請在其上設置默認值。如果您沒有使用不同的實體,請考慮更改以確保您(它們可能是共同父項的子實體)。

您無法將數據插入到映射中。該映射只描述了RestKit應該處理的內容。要編輯您需要自己參與映射過程的值並實施一些委託方法來注入其他數據。

+0

你能解釋一下嗎?實現委託方法來注入更多數據。 Reskit中的哪些方法可以做到這一點。一些例子是完美的。 – digitaljerry

+0

我沒有一個例子,我從來不想涉足這個層面。您需要自己運行映射操作。更好(當然更容易)將是具有默認值的不同實體... – Wain