NSDictionary的介绍和使用

NSDictionary不可变,一旦创建,内容就不能删除和添加(不能改动)

1)创建空字典

NSDictionary *diat = [NSDictionary dictionary];

2)创建只有一组键值对的字典

NSDictionary *dict2 = [NSDictionary dictionaryWithObject:@"zhangsan" ForKey:@"zs"];

3)创建多组键值对的字典,使用value值,key值

NSDictionary *dict3 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1",@"k1",@"value2",@"k2",nil];

注意:

字典的key值和value值都必须是对象

NSLog(@"%@",dict3);//以%@格式打印字典,控制台输出一个{ }

4)快速创建一个字典

@{key值:value值,,,};

key值不能重复:如果重复了也不会报错,如果重复了,最后添加的将不能够保存到字典中

NSDictionary *dict4 = @{@"zs":@"zhaosi",@"zs",@"zhangsan",@"ls":@"lisi",@"bz":@"banzhang"};

NSLog(@"%@",dict4);

5)获取字典的长度

NSLog(@"%lu",dict4.count);

6)根据key值取出value值

NSString *str = [dict4 objectForKey:@"zs"];

NSLog(@"str = %@",str);

7)字典的遍历问题

第一步:获取所有的key

第二步:根据key获取value

for(NSString *key in dict4){

NSLog(@"key = %@,value = %@",key,[dict4 objectForKey:key]);

}

[dict enumerateKeysAndObjectsUsingBlock:^(id key,id obj,BOOL *stop)];

NSLog(@"%@--->%@",key,obj);

1)用简易形式定义一个字典

NSDictionary *dict = @{@"zbz":@"zhangbozhi",@"cgx":@"chengguanxi",@"xzmly":@"hello"}

2)用简易形式,获取key对应的value

NSLog(@“%@”,dict[@"zbz"];)

3)把字典保存到文件中

BOOL isWrite = [dict writeToFile:@"/users/zhaoxiaohu/Desktop/dict.plist" atomically:YES];

if (isWrite){

NSLog(@"写入成功");

}

4)从文件中读取字典

NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:@"/user/zhaoxiaohu/Desktop/dict.plist"];

NSLog(@"readDict = %@",readDict);

--------从文件中读取数组:NSArray *arr = [NSArray arrayWithContentsOfFile:@"/User/sdfkljsdkfajskdfjaskd"];

NSArray *lnArr = [NSArray arrayWithObjects:@"dalian",@"shenyang",nil];

NSArray *sdArr = [NSArray arrayWithObjects:@"jinan",@"weifang",nil];

NSDictionary *citys = [NSDictionary dictionaryWithObjectsAndKeys:lnArr,@"ln",sdArr,@"sd",ni;];

NSLog(@"citys  = %@",citys);

[citys writeToFile:@"/user/sdlkfjklsajdfkl/citys.plist" atomically:YES];

//读取city.plist

NSDictionary *citysDict = [NSDictionary dictionaryWithContentsOfFile:@"/user/sdfsdfsdfdsf/citys.plist"];

遍历

[cityDict enumerateKeysAndObjectsUsingBlock:^(id key,id obj,BOOL *stop){

//NSLog(@"key:%@,value:%@",key,obj);

for(NSString *str in obj) {

NSLog(@"city = %@",str);

}

}];

获取每个数组

NSArray *arr = cityDict[@"ln"];

NSLog(@"%@",[arr lastObject]);

时间: 2024-08-08 19:39:55

NSDictionary的介绍和使用的相关文章

IOS-浅谈通知机制

?每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 ?任何一个对象都可以向通知中心发布通知(NSNotification),描述自己在做什么.其他感兴趣的对象(Observer)可以申请在某个特定通知发布时(或在某个特定的对象发布通知时)收到这个通知,如图: ?一个完整的通知一般包含3个属性: 1- (NSString *)name; // 通知的名称 2- (id)object; // 通知发布者(是谁要发布通知) 3- (NS

IOS开发之旅-IOS常用数据结构NSArray、NSMutableArray、NSDictionary、NSMutableDictionary介绍

NSArray NSArray基本用法 void arrayTest1() { //数组初始化最后必须以nil结尾,表示数组元素结束 NSArray *array1 = [[NSArray alloc]initWithObjects:@"item0",@"item1",@"item2",@"item3",@"item4",nil]; NSLog(@"%@",array1); /*( it

Object-c集合的简单介绍

一.简单介绍 NSArray/NSMutableArray NSSet/NSMutableSet NSDictionary/NSMutableDictionary NSArray.NSSet.NSDictionary是不可变的,创建的时候初始化 NSMutableArray.NSMutableSet.NSMutableDictionary是可变的 二.使用介绍 NSArray是有序的数组 NSMutableArray *myArray=[[NSMutableArray alloc] init];

NSNotificationCenter消息通信机制介绍(KVO)

NSNotificationCenter消息通信机制介绍(KVO) 作用:NSNotificationCenter是专门供程序中不同类间的消息通信而设置的. 注册通知:即要在什么地方接受消息                [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(mytest:) name:@" mytest" object:nil];        参数介绍:         

Cocos2d-x中__Dictionary容器以及实例介绍

__Dictionary类在Cocos2d-x 2.x时代它就是CCDictionary类,它是模仿Objective-C中的NSDictionary类而设计的,通过引用计数管理内存.__Dictionary继承于Ref类,因此它所能容纳的是Ref及子类所创建的对象指针. 1.创建__Dictionary对象 创建__Dictionary对象有很多函数,下面是总结常用的函数: static __Dictionary* create().创建__ Dictionary. static __Dict

Foundation框架介绍

1.Foundation框架介绍 什么是框架? 众多功能\API的集合 框架是由许多类.方法.函数.文档按照一定的逻辑组织起来的集合,以便使研发程序变得更容易,在OS X下的Mac操作系统中大约有80个框架,为所有程序开发奠定基础的框架称为Foundation 框架 Foundation框架的作用 Foundation框架是Mac\iOS中其他框架的基础 Foundation框架包含了很多开发中常用的数据类型: 结构体 枚举 类 如何使用Foundation框架 Foundation框架中大约有

OC学习篇之---Foundation框架中的NSDictionary类以及NSMutableDictionary类

今天来看一下Foundation框架中的NSDictionary类,NSMutableDictionary类,这个和Java中的Map类很想,OC中叫字典,Java中叫Map,还有字典是无序的,这个和NSArray不一样,Java中的Map也是无序的,通过hash值去检索元素的. 一.NSDictionary类 [objc] view plain copy // //  main.m //  19_NSDictionary // //  Created by jiangwei on 14-10-

UITabBarController简单介绍

转自:http://www.cnblogs.com/wendingding/p/3775488.html 一.简单介绍 UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型的例子就是QQ.微信等应?. 二.UITabBarController的使用 1.使用步骤: (1)初始化UITabBarController (2)设置UIWindow的rootViewContr

iOS开发UI篇—UITabBarController简单介绍

一.简单介绍 UITabBarController和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型的例子就是QQ.微信等应?. 二.UITabBarController的使用 1.使用步骤: (1)初始化UITabBarController (2)设置UIWindow的rootViewController为UITabBarController (3)创建相应的子控制器(viewcontroller)