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( ...
随机推荐
- Keil : Contents missmatch at:08000E84H Verify Failed!
Keil 下载时出以下错误: Device: STM32F103VB VTarget = 3.300V State of Pins: TCK: 0, TDI: 0, TDO: 1, TMS: 0, T ...
- vs之Nuget
1.http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx 2.在Nuget控制台中安装特定版本: Install-Package <程序包 ...
- Intent简介
1 Intent概念 1.1 Intent的作用 指明Intent所要启动的对象 提供将要启动对象组件运行需要的数据 组件类型 启动方法 Activity startActivity(Intent i ...
- MySQL订单分库分表多维度查询
转自:http://blog.itpub.net/29254281/viewspace-2086198/ MySQL订单分库分表多维度查询 MySQL分库分表,一般只能按照一个维度进行查询. 以订单 ...
- cocos2d-x NotificationCenter
转自:http://www.xinze.me/cocos2d-x-ccnotificationcenter/ 在前端开发中,JS和as3中都有很好的监听机制, 我们使用Event的addEventLi ...
- Twenty Newsgroups Classification任务之二seq2sparse(3)
接上篇,如果想对上篇的问题进行测试其实可以简单的编写下面的代码: package mahout.fansy.test.bayes.write; import java.io.IOException; ...
- External file changes sync may be slow: Project files cannot be watched (are they under network mount?)
if some files are on a mounted disk: go to Settings | Notifications | File Watcher Messages and tune ...
- pip和easy_install使用方式
easy_install 跟 pip 都是 Python 的套件管理程式,有了它們,在使用 Python 開發程式的時候會帶來不少方便. easy_install 和 pip 有什麼不一樣?據 pip ...
- istringstream
编写程序,将来自一个文件中的行保存在一个vector<string>中,然后使用一个istringstream从vector读取数据成员,每次读取一个单词 #include <ios ...
- js调试技巧 Firefox调试技巧汇总
Firebug入门指南 : http://www.ruanyifeng.com/blog/2008/06/firebug_tutorial.html Firebug控制台详解: htt ...