NSDictionary字典创建,获取,遍历,可变字典的删除 - iOS
字典是以键值对的形式来存储数据 key value
1 NSDictionary 字典
1.1 创建字典,不可变的
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"xiaozhe",@"name", nil];
NSLog(@"dic %@",dic);
2016-08-14 14:44:17.460 07-字典类[2325:547877] dic {
name = xiaozhe;
}
1.2 快捷创建方式
NSDictionary * dic2 = @{ @"one":@"1",@"two":@"2"};
NSLog(@"dic2 %@",dic2);
2016-08-14 14:44:17.461 07-字典类[2325:547877] dic2 {
one = 1;
two = 2;
}
1.3 字典中可以存任意数据类型
字典的顺序不是自然顺序
NSArray * array = @[@"one",@"two"];
NSDictionary * dic3 = @{
@"one":@"1",
@"num":[NSNumber numberWithInt:10],
@"aaa":dic2,
@"bbb":dic,
@"ar1":array
};
NSLog(@"dic3 %@",dic3);
016-08-14 14:44:17.461 07-字典类[2325:547877] dic3 {
aaa = {
one = 1;
two = 2;
};
ar1 = (
one,
two
);
bbb = {
name = xiaozhe;
};
num = 10;
one = 1;
}
1.4 获得字典的长度
NSLog(@"count %ld",dic3.count);
1.5 从字典中取值
NSString * str = [dic3 objectForKey:@"one"];
NSLog(@"str %@",str);
NSDictionary * dicTmp = [dic3 objectForKey:@"aaa"];
NSLog(@"dicTmp %@",dicTmp);
NSArray * arrayTmp = [dic3 objectForKey:@"ar1"];
NSLog(@"arrayTmp %@",arrayTmp);
1.6 遍历
取出所有的key值
NSArray * allkeys = [dic3 allKeys];
NSLog(@"allkeys %@",allkeys);
for (int i = 0; i < allkeys.count; i++)
{
NSString * key = [allkeys objectAtIndex:i];
//如果你的字典中存储的多种不同的类型,那么最好用id类型去接受它
id obj = [dic3 objectForKey:key];
NSLog(@"obj %@",obj);
}
枚举器
NSEnumerator * enumerator = [dic3 objectEnumerator];
id value;
while (value = [enumerator nextObject]) {
NSLog(@"value %@",value);
}
2 NSMutableDictionary 可变字典
2.1 创建一个可变长度字典
NSMutableDictionary * muDic = [[NSMutableDictionary alloc] initWithCapacity:0];
2.2 向字典中存储数据
[muDic setObject:@"1" forKey:@"one"];
[muDic setObject:@"2" forKey:@"two"];
[muDic setObject:@"3" forKey:@"three"];
2.3 删除
[muDic removeObjectForKey:@"one"];
2.4 全部删除
[muDic removeAllObjects];
给一个 Student 类
@interface Student : NSObject
@property (nonatomic,assign) int age;
@property (nonatomic,strong) NSString * name;
- (id)initWithName:(NSString *)name andAge:(int)age;
@end
@implementation Student
- (id)initWithName:(NSString *)name andAge:(int)age
{
if (self = [super init])
{
_name = name;
_age = age;
}
return self;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"name %@ age %d",_name,_age];
}
@end
Student * stu1 = [[Student alloc] initWithName:@"xiaoher" andAge:20];
Student * stu2 = [[Student alloc] initWithName:@"alex" andAge:50];
Student * stu3 = [[Student alloc] initWithName:@"xiaoli" andAge:10];
[muDic setObject:stu1 forKey:@"s1"];
[muDic setObject:stu2 forKey:@"s2"];
[muDic setObject:stu3 forKey:@"s3"];
//在向字典中存储数据的时候,一定要保证key值是唯一的
//[muDic setObject:stu3 forKey:@"s3"];
//[muDic setObject:stu3 forKey:@"s3"];
//[muDic setObject:stu3 forKey:@"s3"];
2.5 使用for循环遍历字典
NSArray * allkeys = [muDic allKeys];
for (int i = 0; i < allkeys.count; i++)
{
NSString * key = [allkeys objectAtIndex:i];
Student * stu = [muDic objectForKey:key];
NSLog(@"stu %@",stu);
};
2.6 使用枚举器
NSEnumerator * enumerator = [muDic objectEnumerator];
Student * tmp;
while (tmp = [enumerator nextObject]) {
NSLog(@"tmp %@",tmp);
}
NSDictionary字典创建,获取,遍历,可变字典的删除 - iOS的更多相关文章
- json对象数组的创建、遍历、添加、删除、修改、js的splice()用法
本文链接:https://blog.csdn.net/houfengfei668/article/details/79843625 )第二种方式:手动构造json对象数组 )for )用splice方 ...
- OC中NSDictionary(字典)、NSMutableDictionary(可变字典)、NSSet(集合)、NSMutableSet(可变集合)得常用方法
字典用于保存具有映射关系数据的集合 一个key—value对认为是一个条目(entry),字典是存储key—value对的容器 与数组不同,字典靠key存取元素 key不能重复,value必须是对象 ...
- OC基础 可变字典与不可变字典的使用
OC基础 可变字典与不可变字典的使用 1.不可变字典 1.1创建不可变字典 //创建字典 //注意: //1,元素个数是偶数 //2,每两个元素是一个键值对 //3,值在前,键在后 NSDiction ...
- python中字典dic详解-创建,遍历和排序
原文地址:http://www.bugingcode.com/blog/python_dic_create_sort.html 在python的编程中,字典dic是最典型的数据结构,看看如下对字典的操 ...
- Objective-C 字典、可变字典
字典相当于c++ stl中的map 字典NSDictionary #import <UIKit/UIKit.h> #import "AppDelegate.h" int ...
- iosOC不可变字典和可变字典
//key 和 value 都属于(id)对象类型 //key常用字符串NSString来表示 //存储数值型 一般可用 NSString //int age ->@(age) // [di ...
- objective-c可变字典
1 #pragma mark *****************************字典******************************** 2 // 字典:通过ke ...
- python3字典:获取json响应值来进行断言
前言:在接口自动化测试中,最后都是需要将返回结果进行断言.本文按照实际情况举例说明字典各种操作,以及如何提取响应数据来进行断言 1.dict = {'code': '200', 'message': ...
- python3字典:获取json响应值来进行断言的用法详解
在Python中我们做接口经常用到一些json的返回值我们常把他转化为字典,在前面的python数据类型详解(全面)中已经谈到对字典的的一些操作,今天我们就获取json返回值之后,然后转化为字典后的获 ...
随机推荐
- 森林 BZOJ 3123
题解: 第k大直接用主席树解决 合并利用启发式合并,将较小的连接到较大的树上 #include<cmath> #include<cstdio> #include<cstd ...
- hdu 5037 Frog 贪心 dp
哎,注意细节啊,,,,,,,思维的严密性..... 11699193 2014-09-22 08:46:42 Accepted 5037 796MS 1864K 2204 B G++ czy Frog ...
- gitweb 搭建教程
1. 前言 git 是一个版本控制工具,类似svn. 本文内容主要涉及git仓库通过浏览器访问(用web的方式去查看git提交历史记录,tag,branch等信息),即gitweb. 效果图: 在这里 ...
- (12)centos之stmp服务器
yum remove sendmail #卸载sendmail
- electron 自定义菜单
快捷键:http://electronjs.org/docs/api/accelerator
- BZOJ——3296: [USACO2011 Open] Learning Languages
http://www.lydsy.com/JudgeOnline/problem.php?id=3296 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: ...
- HDOJ 5213
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5213 BC 上的题,题解很清楚,会莫对的应该不难, 对于一个询问,我们拆成四个询问,开始拆成求区间矩形 ...
- [Bzoj5254][Fjwc2018]红绿灯(线段树)
5254: [Fjwc2018]红绿灯 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 31 Solved: 24[Submit][Status][D ...
- stream_context_create()模拟POST/GET
有时候,我们需要在服务器端模拟 POST/GET 等请求,也就是在 PHP 程序中去实现模拟,该怎么做到呢?或者说,在 PHP 程序里,给你一个数组,如何将这个数组 POST/GET 到另外一个地址呢 ...
- 一道有关switch-case题目
一道有关switch-case题目 /** * * @title:SwitchCase.java * @Package:com.you.hbxs.model * @Description:<h3 ...