Sian 发表于 2014-3-23 23:51:16

可变字典的基本使用,及字典的遍历

//
//main.m
//NSMutableDictionary
//
//Created by yusian on 14-3-23.
//Copyright (c) 2014年 小龙虾论坛. All rights reserved.
//

#import <Foundation/Foundation.h>

int main()
{
    // 创建一个可变字典
    NSMutableDictionary *dict = ;
   
    // 添加键值对
    ;
   
    ;
   
    // 移除键值对
    // ;
    // ;
    // ;
   
    // 字典打印
    NSLog(@"%@", dict);
   
    // 字典的遍历之for循环
    for (int i = 0; i < dict.count; i++) {
   
      // 先取出所有的key
      NSArray * keys = ;
      
      // 每次循环都从数组中取出一个key值
      NSString *key = keys;
      
      // 根据每次取出的key值,找到对应的键值
      NSString *obj = dict;
      
      // 输出
      NSLog(@"%@ - %@", key, obj);
    }
   
    // 字典遍历之遍历方法(传block方式),具体实现在数组的遍历方法中有详细研究过
    [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
      
      // 可直接使用key与obj
      NSLog(@"%@ - %@", key, obj);
      
      // 如果key值等于name则遍历终止
      if () {
            
            *stop = YES;
            
      }
    }];

    return 0;
}


页: [1]
查看完整版本: 可变字典的基本使用,及字典的遍历