开发者

Getting length of an array inside a Dictionary Key

I'm sure this is a very obvious question but开发者_如何学运维 I'm not getting anywhere with it and I've been trying for half an hour or so now.

I have an NSMutableDictionary which has keys & values, obviously. Each key stores an array of objects. What I need to do is find a specific array in a key and get the list of the array. The catch is that I don't know the value of the key, I just know it's index. (EG: I know I need to find the array in the 2nd key).

I am almost certain this is a very easy & trivial thing to do but it's escaping me, I've only been doing Obj-C for a short while so not entirely at home with it yet!

Thanks,

Jack.


Use allKeys: to access the keys of your dictionary.

- (NSArray *)allKeys

Use as below .

    NSArray* dictAllKeys = [dict allKeys];
    if([dictAllKeys count] > 2)
    {
      NSArray* myArrayInDict = [dict objectForKey:[dictAllKeys objectAtIndex:1]];
      // get the length of array in dict at 2nd key
      int length = [myArrayInDict count];
    }


The order will probably change if another key/value pair is added, it is a NSMutableDictionary. It is best not to rely on the order of a NSDictionary or NSSet.

Suggestion: Either use another container that does provide ordering such as NSMutableArray or find the item using the dictionary's value arrays, perhaps with NSPredicate.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜