OC3_字典
//
// main.m
// OC3_字典
//
// Created by zhangxueming on 15/6/12.
// Copyright (c) 2015年 zhangxueming. All rights reserved.
// #import <Foundation/Foundation.h>
//NSDictionary
//创建一个不可变字典对象
//NSMutableDictionary
//创建一个可变字典对象 //字典中对象都是键值对
//key:value
//字典中的键值对没有顺序
//字典中的key是唯一的, 但是value不一定是唯一的 int main(int argc, const char * argv[]) {
@autoreleasepool {
//用value 及 key 构造字典对象
NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"",@"one",@"",@"two",@"",@"three",@"",@"four",@"",@"five",@"",@"six", nil];
NSLog(@"dict = %@", dict);
//创建常量字典对象
NSDictionary *dict2 = @{@"one":@"",@"two":@""};
NSLog(@"dict2 = %@", dict2); //用传入的字典对象创建字典对象
NSDictionary *dict3 = [[NSDictionary alloc] initWithDictionary:dict];
NSLog(@"dict3 = %@", dict3); //用传入的value数组及key数组创建字典对象
NSDictionary *dict4 = [[NSDictionary alloc] initWithObjects:@[@"",@"",@"",@""] forKeys:@[@"three",@"four",@"five",@"six"]];
NSLog(@"dict4 = %@", dict4); //创建一个空的字典对象
NSDictionary *dict5 = [[NSDictionary alloc] init];
NSLog(@"dict5 = %@", dict5); //类方法创建字典对象
//跟initWithObjectsAndKeys:方法对应
NSDictionary *dict6 = [NSDictionary dictionaryWithObjectsAndKeys:@"",@"one",@"",@"two",@"",@"three", nil];
NSLog(@"dict6 = %@", dict6);
//跟initWithObjects:forKeys:相对应
NSDictionary *dict7 = [NSDictionary dictionaryWithObjects:@[@"",@"",@"",@""] forKeys:@[@"three",@"four",@"five",@"six"]];
NSLog(@"dict7 = %@", dict7); //跟initWithDictionary:相对应
NSDictionary *dict8 = [NSDictionary dictionaryWithDictionary:dict7];
NSLog(@"dict8 = %@", dict8); //计算字典中键值对的个数
NSUInteger len = [dict count];
NSLog(@"len = %li", len); //获取key对应的值
id value = [dict objectForKey:@"two"];
NSLog(@"value = %@", value); //获取所有的key
NSArray *keys = [dict allKeys];
NSLog(@"keys = %@", keys); //获取值对应所有的key
NSArray *keys2 = [dict allKeysForObject:@""];
NSLog(@"keys2 = %@", keys2); //获取所有的值
NSArray *values = [dict allValues];
NSLog(@"values = %@", values); //判断两个字典是否相等
BOOL ret = [dict isEqualToDictionary:dict2];
NSLog(@"ret = %i", ret); //遍历字典 -- 实际上是遍历字典的key //枚举器法
NSEnumerator *keysArray = [dict keyEnumerator];
for (id item in keysArray) {
NSLog(@"%@:%@",item,[dict objectForKey:item]);
}
//快速枚举法
for (id key in dict) {
NSLog(@"%@:%@", key, [dict objectForKey:key]);
}
//只遍历字典中的值
NSEnumerator *valuesArray = [dict objectEnumerator];
for (id item in valuesArray) {
NSLog(@"vlaue = %@", item);
} //可变字典操作
//构造指定容量大小的字典对象
//+ (instancetype)dictionaryWithCapacity:(NSUInteger)numItems;
NSMutableDictionary *mulDict = [[NSMutableDictionary alloc] initWithCapacity:];
NSLog(@"mulDict = %@",mulDict); //删除指定的key对应的键值对
NSMutableDictionary *mulDict2 = [NSMutableDictionary dictionaryWithDictionary:@{@"one":@"",@"two":@"",@"three":@"",@"four":@""}];
[mulDict2 removeObjectForKey:@"two"];
NSLog(@"mulDict2 = %@", mulDict2);
//添加(或者修改)键值对
[mulDict2 setObject:@"" forKey:@"three"];
NSLog(@"mulDict2 = %@", mulDict2); //将传入字典中的键值对添加到字典中 相同的修改,不同的就添加
[mulDict2 addEntriesFromDictionary:dict3];
NSLog(@"mulDict2 = %@", mulDict2); //删除所有的键值对
[mulDict2 removeAllObjects];
NSLog(@"mulDict2 = %@", mulDict2); //删除key数组对应所有键值对
NSMutableDictionary *mulDict3 = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@"",@"five",@"",@"six",@"",@"seven",@"",@"eight", nil];
[mulDict3 removeObjectsForKeys:@[@"five",@"eight",@"nine"]];
NSLog(@"mulDict3 = %@", mulDict3);
//重置字典对象
[mulDict3 setDictionary:@{@"hello":@"",@"world":@""}];
NSLog(@"mulDict3 = %@", mulDict3); }
return ;
}
OC3_字典的更多相关文章
- 【DG】Oracle_Data_Guard官方直译
[DG]Oracle Data Guard官方直译 1 Oracle Data Guard 介绍 Oracle Data Guard概念和管理10g版本2 Oracle Data Guard ...
- DVWA实验之Brute Force(暴力破解)- Low
DVWA实验之Brute Force-暴力破解- Low 这里开始DVWA的相关实验~ 有关DVWA环境搭建的教程请参考: https://www.cnblogs.com/0yst3r-2 ...
- Oracle错误览表
Oracle 错误总结及问题解决 ORA 本文转自:https://www.cnblogs.com/zhangwei595806165/p/4972016.html 作者@承影剑 ORA-0 ...
- Javacript实现字典结构
字典是一种用[键,值]形式存储元素的数据结构.也称作映射,ECMAScript6中,原生用Map实现了字典结构. 下面代码是尝试用JS的Object对象来模拟实现一个字典结构. <script& ...
- python 数据类型 ----字典
字典由一对key:value 组成的 python中常用且重量级的数据类型 1. key , keys, values 字典由一对key:value 组成的 python中常用且重量级的数据类型 1. ...
- 增强版字典DictionaryEx
代码 public class DictionaryEx<TKey, TValue> : IDictionary<TKey, TValue> { /// <summary ...
- python学习笔记(字符串操作、字典操作、三级菜单实例)
字符串操作 name = "alex" print(name.capitalize()) #首字母大写 name = "my name is alex" pri ...
- python之最强王者(8)——字典(dictionary)
1.Python 字典(Dictionary) 字典是另一种可变容器模型,且可存储任意类型对象. 字典的每个键值(key=>value)对用冒号(:)分割,每个对之间用逗号(,)分割,整个字典包 ...
- python序列,字典备忘
初识python备忘: 序列:列表,字符串,元组len(d),d[id],del d[id],data in d函数:cmp(x,y),len(seq),list(seq)根据字符串创建列表,max( ...
随机推荐
- Cocos2d-x 重写draw方法绘制直线等图形时被遮挡覆盖问题的一种解决方案
最近在学习过程cocos2dx的过程中需要使用到绘制直线的功能,所以我就采用了引擎中 ccDrawLine 方法,然后重写 draw 方法,在该方法中绘制直线. 但是出现了一个问题,那就是绘制的图形被 ...
- NoInstall_Mysql
安装卸载一直是mysql比较头疼的问题,前几天得知可以用绿色版的mysql,解决了这一难题.
- The Sorrows of Young Werther
The Sorrows of Young Werther J.W. von Goethe Thomas Carlyle and R.D. Boylan Edited by Nathen Haskell ...
- 获取Application中的spring容器
方式1: ApplicationContext ac = (ApplicationContext) invocation.getInvocationContext().getApplication() ...
- 修改cas登陆页面-服务器端
原文地址:http://www.cnblogs.com/liveandevil/archive/2013/03/06/2946341.html 1.cas统一认证的登陆页面位于:cas目录/WEB-I ...
- 设置UITabBarController的背景颜色
if (IOS7) { self.tabBarController.tabBar.barTintColor = kTAB_BAR_GB_COLOR; }else{ self.tabBarControl ...
- 【转】suid sgid 详解
转自:http://www.cnblogs.com/fhefh/archive/2011/09/20/2182155.html 如果你对SUID.SGID仍有迷惑可以好好参考一下! Copyright ...
- C++ 继承的访问权限
1.它解决什么问题?为什么设计出继承的各种访问权限? 可以这样认为,C++继承会把父类的东西拉到自己这里,这些东西都是自己的,父类中的字段和方法都有访问权限,如果我想改变这些东西的访问权限,该怎么办? ...
- js 如何将无限级分类展示出来
这个需要运用递归. 案例:将数据以 ul li ul li形式展现在div中. <div id="div"></div> 数据格式为json: var da ...
- Tomcat部署web应用的三种方式
原文:http://my.oschina.net/sunchp/blog/90235 一:相关概念 CATALINA_HOME:tomcat安装目录 CATALINA_BASE:tomcat工作目录 ...