首页 > 代码库 > NSMutableDictionary--可变字典
NSMutableDictionary--可变字典
// // 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 = http://www.mamicode.com/dic[key];>NSMutableDictionary--可变字典
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。