Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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中的数组元素计数_Objective C_Json_Collections_Restkit - Fatal编程技术网

Objective c 映射RestKit中的数组元素计数

Objective c 映射RestKit中的数组元素计数,objective-c,json,collections,restkit,Objective C,Json,Collections,Restkit,我正在使用RestKit 0.23 考虑以下带有JSON主体的HTTP响应 { id: 1, description: "abc", images: [ { id: 1, ... a lot of other attributes that I do not need in fact ... } ] } 我定义了以下映射 RKObjectMapping *responseImageM

我正在使用RestKit 0.23

考虑以下带有JSON主体的HTTP响应

{
    id: 1,
    description: "abc",
    images: [
        {
            id: 1,
            ... a lot of other attributes that I do not need in fact ...
        }
    ]
}
我定义了以下映射

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"}];


谢谢。

我没有试过,但您应该有两个选项,都使用KVC:

最好的方法是使用
@“图像。@计数”:@“图像计数”

另一种方法是使用自定义访问器方法,将整个images数组传递给它,并允许它进行计数(或执行其他操作,如随机项)
@“images”:@“countImages”

(注意,在自定义访问器方法中,您需要将setter定义为getter,即使您永远不会使用getter)

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