用于Objective-C中用于访问NSMutable字典的每个循环 [英] for each loop in Objective-C for accessing NSMutable dictionary

查看:94
本文介绍了用于Objective-C中用于访问NSMutable字典的每个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在访问Objective-C中的可变字典键和值时遇到了一些困难.

I am finding some difficulty in accessing mutable dictionary keys and values in Objective-C.

假设我有这个:

NSMutableDictionary *xyz=[[NSMutableDictionary alloc] init];

我可以设置键和值.现在,我只想访问每个键和值,但是我不知道设置的键数.

I can set keys and values. Now, I just want to access each key and value, but I don't know the number of keys set.

在PHP中,它非常简单,如下所示:

In PHP it is very easy, something as follows:

foreach ($xyz as $key => $value)

在Objective-C中怎么可能?

How is it possible in Objective-C?

推荐答案

for (NSString* key in xyz) {
    id value = xyz[key];
    // do stuff
}

此方法适用于所有符合NSFastEnumeration协议的类(在10.5+和iOS上可用),尽管NSDictionary是少数几个可以枚举键而不是值的集合之一.我建议您阅读有关快速枚举 Collections编程主题中.

This works for every class that conforms to the NSFastEnumeration protocol (available on 10.5+ and iOS), though NSDictionary is one of the few collections which lets you enumerate keys instead of values. I suggest you read about fast enumeration in the Collections Programming Topic.

哦,我要补充一点,您应该从不在枚举集合时对其进行修改.

Oh, I should add however that you should NEVER modify a collection while enumerating through it.

这篇关于用于Objective-C中用于访问NSMutable字典的每个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆