在数组中访问NSDictionary

在数组中访问NSDictionary

本文介绍了在数组中访问NSDictionary的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从数组(从JSON字符串)访问所需数据时遇到一些问题

Im having a few problems accessing the required data from an array (which is from a JSON string)

JSON数组中的数据存储在PatternImagesArray中

The data from the JSON array is stored in the PatternImagesArray

我正在收藏视图中显示图像,并希望计算一个部分中的项目数.

Im displaying images within a collection view and want to count the number of items within a section.

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    //return [self.patternImagesArray count];

    NSDictionary *itemsInSection = [self.patternImagesArray objectAtIndex:section];

    NSInteger *ItemCount = [itemsInSection count];


    return ItemCount;


}

这是JSON字符串

[{"RequestTitle":[{"ID":"16","path":"upload\/images\/14439822460482106558105.jpg","thumb_path":""},{"ID":"50","path":"upload\/images\/1445170810100.jpg","thumb_path":"upload\/images\/thumb_1445170810100.jpg"},{"ID":"51","path":"upload\/images\/1445172097564.jpg","thumb_path":"upload\/images\/thumb_1445172097564.jpg"},{"ID":"60","path":"upload\/images\/1445184745267.jpg","thumb_path":"upload\/images\/thumb_1445184745267.jpg"},{"ID":"61","path":"upload\/images\/1445184815796.jpg","thumb_path":"upload\/images\/thumb_1445184815796.jpg"},{"ID":"62","path":"upload\/images\/1445184872026.jpg","thumb_path":"upload\/images\/thumb_1445184872026.jpg"},{"ID":"64","path":"upload\/images\/1445192930295.jpg","thumb_path":"upload\/images\/thumb_1445192930295.jpg"},{"ID":"65","path":"upload\/images\/1445260759823.jpg","thumb_path":"upload\/images\/thumb_1445260759823.jpg"},{"ID":"66","path":"upload\/images\/1446759963249.jpg","thumb_path":"upload\/images\/thumb_1446759963249.jpg"},{"ID":"67","path":"upload\/images\/1446760122026.jpg","thumb_path":"upload\/images\/thumb_1446760122026.jpg"},{"ID":"69","path":"upload\/images\/","thumb_path":"upload\/images\/thumb_"},{"ID":"70","path":"upload\/images\/1446760923270.jpg","thumb_path":"upload\/images\/thumb_1446760923270.jpg"},{"ID":"78","path":"upload\/images\/1447504226902.jpg","thumb_path":"upload\/images\/thumb_1447504226902.jpg"},{"ID":"82","path":"upload\/images\/1447934170005.jpg","thumb_path":"upload\/images\/thumb_1447934170005.jpg"}]},{"RequestTitle":[{"ID":"19","path":"upload\/images\/1430571954572.jpg","thumb_path":""},{"ID":"52","path":"upload\/images\/1445173074393.jpg","thumb_path":"upload\/images\/thumb_1445173074393.jpg"},{"ID":"53","path":"upload\/images\/1445173124081.jpg","thumb_path":"upload\/images\/thumb_1445173124081.jpg"},{"ID":"77","path":"upload\/images\/1446818706307.jpg","thumb_path":"upload\/images\/thumb_1446818706307.jpg"},{"ID":"80","path":"upload\/images\/1447504506629.jpg","thumb_path":"upload\/images\/thumb_1447504506629.jpg"}]},{"RequestTitle":[{"ID":"21","path":"upload\/images\/security.jpeg","thumb_path":""},{"ID":"54","path":"upload\/images\/","thumb_path":"upload\/images\/thumb_"},{"ID":"55","path":"upload\/images\/1445173189403.jpg","thumb_path":"upload\/images\/thumb_1445173189403.jpg"},{"ID":"63","path":"upload\/images\/1445184942890.jpg","thumb_path":"upload\/images\/thumb_1445184942890.jpg"},{"ID":"68","path":"upload\/images\/1446760628765.jpg","thumb_path":"upload\/images\/thumb_1446760628765.jpg"}]},{"RequestTitle":[{"ID":"20","path":"upload\/images\/1443975202043-1048396424.jpg","thumb_path":""},{"ID":"56","path":"upload\/images\/","thumb_path":"upload\/images\/thumb_"},{"ID":"57","path":"upload\/images\/1445173301456.jpg","thumb_path":"upload\/images\/thumb_1445173301456.jpg"},{"ID":"58","path":"upload\/images\/","thumb_path":"upload\/images\/thumb_"},{"ID":"59","path":"upload\/images\/1445177540605.jpg","thumb_path":"upload\/images\/thumb_1445177540605.jpg"},{"ID":"79","path":"upload\/images\/1447504348072.jpg","thumb_path":"upload\/images\/thumb_1447504348072.jpg"}]}]

变量patternImageArray的结构类似

the variable patternImageArray i structured like so

_patternImagesArray __NSCFArray *   @"4 objects"    0x00000001546e48e0
[0] __NSCFDictionary *  1 key/value pair    0x00000001546e3510
[0] struct __lldb_autogen_nspair
key __NSCFString *  @"RequestTitle" 0x0000000154612fa0
value   __NSCFArray *   @"14 objects"   0x00000001546e3470
[0] __NSCFDictionary *  3 key/value pairs   0x000000015466c2a0
[1] __NSCFDictionary *  3 key/value pairs   0x00000001546e1f70
[2] __NSCFDictionary *  3 key/value pairs   0x00000001546e28b0
[3] __NSCFDictionary *  3 key/value pairs   0x00000001546c1230
[4] __NSCFDictionary *  3 key/value pairs   0x00000001546e2960
[5] __NSCFDictionary *  3 key/value pairs   0x00000001546e2a30
[6] __NSCFDictionary *  3 key/value pairs   0x00000001546e3040
[7] __NSCFDictionary *  3 key/value pairs   0x00000001546e16f0
[8] __NSCFDictionary *  3 key/value pairs   0x00000001546e1830
[9] __NSCFDictionary *  3 key/value pairs   0x00000001546e1970
[10]    __NSCFDictionary *  3 key/value pairs   0x00000001546e1a70
[11]    __NSCFDictionary *  3 key/value pairs   0x00000001546e3190
[12]    __NSCFDictionary *  3 key/value pairs   0x00000001546e32b0
[13]    __NSCFDictionary *  3 key/value pairs   0x00000001546e33f0
[1] __NSCFDictionary *  1 key/value pair    0x00000001546e3bf0
[2] __NSCFDictionary *  1 key/value pair    0x00000001546e41e0
[3] __NSCFDictionary *  1 key/value pair    0x00000001546e4860

我需要访问状态为14项的节点

I need to access the node which states that are 14 items

预先感谢

推荐答案

有人张贴了已删除的评论,不知道为什么,因为它是正确的答案

Someone posted a comment that was removed, not sure why as it was the right answer

用于计数数组/nsditontonary的校正后的代码是

the corrected code for counting the array/nsdicitonary is

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    //return [self.patternImagesArray count];

    NSDictionary *itemsInSection = [self.patternImagesArray objectAtIndex:section];

    NSInteger *ItemCount = [[itemsInSection allValues][0] count];


    return ItemCount;


}

希望这对其他人有帮助

这篇关于在数组中访问NSDictionary的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 06:45