首页 > 学院 > 开发设计 > 正文

遍历字典

2019-11-07 23:25:17
字体:
来源:转载
供稿:网友
1、使用for-each循环遍历字典中的对象,可以通过如下代码实现:for (NSString *s in [dictionary allValues]) {    NSLog(@"value: %@", s);}2、NSDictionary函数allValues会返回以数组而非字典形式组织的对象。函数allKeys会将键值作为数组返回:for (NSString *s in [dictionary allKeys]) {    NSLog(@"key: %@", s);}3、通过enumerateKeysAndObjectsUsingBlock:方法针对字典中的每个对象执行代码。可以用来定义代码块,然后应用到字典中的每个对象,同时又不必创建for-each循环或是获得数组版本的字典引用:[dictionary enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {    NSLog(@"key = %@ and obj = %@", key, obj);}];
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表