标签:
// // main.m // OC05-task-02 // // Created by Xin the Great on 15-1-25. // Copyright (c) 2015年 Xin the Great. All rights reserved. // #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... ///////////////NSMutableDictionary--可变字典/////////////// //初始化可变字典 //空的字典 NSMutableDictionary *dic1 = [NSMutableDictionary dictionary]; NSLog(@"dic1 is %@", dic1); //给字典预期的一个空间 NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithCapacity:10]; //添加元素 [dic1 setObject:@"value1" forKey:@"key1"]; [dic1 setObject:@"value2" forKey:@"key2"]; [dic1 setObject:@"value3" forKey:@"key3"]; //设置键值对,如果key已经存在,则是修改key所对应的value, 如果不存在,则创建一个新的键值对 [dic1 setObject:@"value4" forKey:@"key4"]; NSLog(@"dic1 is %@", dic1); //删除元素 //删除指定的元素 [dic1 removeObjectForKey:@"key2"]; NSLog(@"dic1 is %@", dic1); //删除所有的元素 [dic1 removeAllObjects]; NSLog(@"dic1 is %@", dic1); //字典的遍历 NSDictionary *dic = @{@"k1":@"v1", @"k2":@"v2", @"k3":@"v3", @"k4":@"v4", @"k5":@"v5",}; //传统遍历 NSArray *keys = [dic allKeys]; for (int i = 0; i < dic.count; i++) { //获取key NSString *key = keys[i]; NSString *value = dic[key]; NSLog(@"value[%@] is %@", key, value); } NSLog(@"------------------------------"); //快速遍历, 快速遍历效率要高于传统遍历 for (NSString *key in dic) { NSString *value = dic[key]; NSLog(@"value[%@] is %@", key, value); } } return 0; }
标签:
原文地址:http://blog.csdn.net/zuojx1013/article/details/43163737