2014-12-04 112 views
0

我正在使用RestKit 0.23。映射RestKit中的數組元素數

考慮具有JSON體

{ 
    id: 1, 
    description: "abc", 
    images: [ 
     { 
      id: 1, 
      ... a lot of other attributes that I do not need in fact ... 
     } 
    ] 
} 

以下HTTP響應我有以下映射定義

RKObjectMapping *responseImageMapping = [RKObjectMapping mappingForClass: [NoteImage class]]; 
[responseImageMapping addAttributeMappingsFromDictionary: @{@"id": @"imageId"}]; 

RKObjectMapping *responseMapping = [RKObjectMapping mappingForClass: [Note class]]; 
[responseMapping addAttributeMappingsFromDictionary: @{@"id": @"noteId", 
                 @"description": @"noteDescription"}]; 
[responseMapping addPropertyMapping: [RKRelationshipMapping relationshipMappingFromKeyPath: @"images" toKeyPath: @"images" withMapping: responseImageMapping]]; 

此代碼工作正常。 但是,響應包含的數據比我真正需要的要多得多。我對圖像的任何屬性都不感興趣。我只需要區分空陣列圖像和非空陣列圖像。

如果我只需要知道圖像數組是否爲空,是否必須定義類NoteImage及其映射?

我不知道如果我能寫類似

[responseMapping addAttributeMappingsFromDictionary: @{@"images#isEmpty": @"imagesEmpty"}]; 

[responseMapping addAttributeMappingsFromDictionary: @{@"images#count": @"imageCounty"}]; 

感謝。

回答

0

我還沒有試過,但你應該有2個選擇,使用KVC兩個:

最好是collection operators所以你可以使用@"[email protected]" : @"imageCount"

替代是使用自定義的訪問方法,通過它的整個圖像陣列,並允許它採取計數(或做其他事情,如採取隨機物品)@"images" : @"countImages"

(請注意,在自定義訪問器方法的方法,你需要定義一個setter一個吸氣劑,即使你永遠不會使用吸氣劑)

+0

'@「images。@ count」'完美地工作。謝謝。 – Cimlman 2014-12-11 08:23:06