------- IOS开发、IOS培训、期待与您交流! -------
* 快速创建(不可变):@{key1 : value1, key2 : value2}
* 快速访问元素:字典名[key]
索引 ----> 文字内容
里面存储的东西都是键值对
/*------------------------------创建可变字典----------------------*/
//创建
// 1、键值对分别都用数组存放方式
NSArray *keys = @[@"name",@"QQ",@"address"];
NSArray *objcets = @[@"Jack",@"21653462",@"Beijing"];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:objcets forKeys:keys];
id obj = [dict objectForKey:@"name"];
// id obj = dict[@"name"];
NSLog(@"%@",obj);
//返回的是键值对的个数,这里是 3
NSLog(@"%ld",dict.count);
// 输出所有的key
NSLog(@"%@", dict.allKeys);
// 是以QQ = 21653462;这种形式打印
NSLog(@"%@",dict);
// 2、ObjectsAndKeys同时罗列出来这种方式,注意这种方式返回的类型是不可变的
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"jack",@"name",
@"Beijin",@"address",
@"2413241",@"QQ",nil];
// 3、快捷方式,编译器会自动转成上面的方式,注意这种方式返回的类型是不可变的
NSDictionary *dict = @{@"name": @"jack",
@"address": @"Beijing",
@"QQ": @"2413241",
};
// 添加字典,
// 1、如果Key重复了,会覆盖之前的值;
// 2、一个key只能对应一个object,但是一个object可以对应多个key
NSMutableDictionary *dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:@"One" forKey:@"1"];
[dictionary setObject:@"Two" forKey:@"2"];
[dictionary setObject:@"Three" forKey:@"3"];
[dictionary setObject:@"Four" forKey:@"4"];
NSLog(@"dictionary:%@",dictionary);
//删除指定的字典
[dictionary removeObjectForKey:@"3"];
NSLog(@"dictionary:%@",dictionary);
/*------------------------------ 遍历字典 ----------------------*/
// 1、利用for循环来遍历字典
// 取出所有的key,虽然存在数组里,但实际还是无序的
NSArray *keys = [dict allKeys];
for (int i = 0; i<dict.count; i++) {
NSString *key = keys[i];
NSString *object = dict[key];
NSLog(@"%@ = %@", key, object);
}
// 2、利用方法来遍历字典,比较好
[dict enumerateKeysAndObjectsUsingBlock:
// 每遍历到一个元素,就会调用一次block
// 并且当前元素和索引位置当做参数传给block
^(id key, id obj, BOOL *stop) {
NSLog(@"%@ - %@", key, obj);
}];
黑马视屏,汤姆猫里的一段:
//1.获得 tom.plist的全路径
// 如果要访问项目中资源包里面的所有资源,应该用mainBundle
NSBundle *bundle = [NSBundle mainBundle];
// 获得文件的全路径
NSString *path = [bundle pathForResource:@"tom" ofType:@"plist"];
//2.根据文件路径加载字典
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
//NSLog(@"%@",dict);
//3.取出按钮文字
NSString *title = [sender titleForState:UIControlStateNormal];
int count = [dict[title] intValue];
// 1.加载plist,array中装得都是NSDictionary对象
NSString *path = [[NSBundle mainBundle] pathForResource:@"shops" ofType:@"plist"];
// 加载path对应的文件来创建数组
NSArray *array = [NSArray arrayWithContentsOfFile:path];
将字典导入.plist 文件中存放
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString* plistfile1 = [[NSBundle mainBundle]pathForResource:@Property List ofType:@plist];
NSMutableDictionary* data = [[NSMutableDictionary alloc]initWithContentsOfFile:plistfile1];
//打印出字典里的数据
NSLog(@%@,data);
[data setObject:@add some content forKey:@c_key];
//获取应用沙盒的Douch
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString* plist1 = [paths objectAtIndex:0];
//获取一个plist文件
NSString* filename = [plist1 stringByAppendingString:@test.plist];
[data writeToFile:filename atomically:YES];
NSMutableDictionary* data1 = [[NSMutableDictionary alloc]initWithContentsOfFile:filename];
//打印出字典里的数据
NSLog(@%@,data1);
//修改一个plist文件的数据
[data1 setObject:@要修改的数值 forKey:@要修改的name];
[data1 writeToFile:filename atomically:YES];
}