在数组中访问 NSDictionary

Posted

技术标签:

【中文标题】在数组中访问 NSDictionary【英文标题】:Accessing NSDictionary within array 【发布时间】:2015-11-23 15:43:49 【问题描述】:

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

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

我在一个集合视图中显示图像,并想计算一个部分中的项目数。

-(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 我的结构是这样的

_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 项的节点

提前致谢

【问题讨论】:

看起来itemsInSection 是一个字典,其中包含一个键值对,其值是一个包含 14 个项目的数组。 ItemCount 应该是 NSInteger,而不是 NSInteger*。它不是一个对象。约定是小写骆驼,所以变量应该命名为 itemCount。 【参考方案1】:

有人发表了一条被删除的评论,不知道为什么,因为它是正确的答案

用于计算数组/nsdicitonary 的更正代码是

-(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的主要内容,如果未能解决你的问题,请参考以下文章

在vuejs中访问数组内部的数组值

在数组中访问 NSDictionary

在ejs中访问传递的数组元素

在 Javascript 中访问数组

在算法的上下文中啥构成“数组访问”?

在 MASM 中访问数组