Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

ios - Accessing NSDictionary within array

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

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;


}

here is the JSON string

[{"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"}]}]

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

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

Thanks in advance

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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;


}

hope this helps someone else


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...