天天看点

OC学习----字典

NSDictionary / NSMutableDictionary

1.概念:字典是关键字

key

及其定义的值

value

所构成的集合.即它是由key-value组成的键值队的集合.通常来讲,

key

一般是

NSString

类型,

value

是OC中任意的对象.对于字典来讲,我们就是通过

key

来访问

value

.

2.为什么在有了数组后,还需要有字典呢?在有大量的数据或频繁的查找信息时,使用字典的话,效率更高.因为字典是采用键查询的优化存储方式.

3.在OC中,我们使用

NSDictionary

NSMutableDictionary

来表示字典.其中,

NSMutableDictionary

NSDictionary

的子类.

4.不可变字典初始化

NSDictionary *dictionary1 = [NSDictionary dictionary];
    NSDictionary *dictionary2 = [[NSDictionary alloc] init];
    //创建一个健值对的字典
    NSDictionary *dictionary3 = [NSDictionary dictionaryWithObject:@"aaa" forKey:@"name"];
    //创建多个健值对的字典
    NSDictionary *dictionary4 = [NSDictionary dictionaryWithObjectsAndKeys:@"aaa",@"name",@24,@"age",@"111",@"address", nil];
    //value在前,key在后,value和key成双出现
    NSLog(@"%@",dictionary4);
    //根据一个字典创建一个字典
    NSDictionary *dictionary5 = [NSDictionary dictionaryWithDictionary:dictionary4];

    //新语法创建字典
    NSDictionary *dictionary6 = @{};//空字典
    NSDictionary *dictionary7 = @{@"name":@"yanshuxin"};
    NSDictionary *dictionary8 = @{@"name":@"yanshuxin",@"age":@21,@"address":@"广州"};
    NSLog(@"%@",dictionary8);

    NSDictionary *dictionary10 = [NSDictionary dictionaryWithContentsOfFile:@""];
    NSDictionary *dictionary11 = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@""]];
           

5.如何访问字典的数量

//求字典中健值对的数量
    NSInteger count = dictionary4.count;
    NSLog(@"%ld",count);
           

6.如何访问字典中元素

//通过key来访问字典中对应的值
    NSString *name = [dictionary4 objectForKey:@"name"];
    NSLog(@"%@",name);
    //新语法
    NSString *newName = dictionary4[@"name"];
    NSLog(@"%@",newName);

    //如何遍历字典中的所有值?
    //表示字典中所有的key
    NSArray *keyArray = dictionary4.allKeys;
    //表示字典中所有的value
    NSArray *valueArray = dictionary4.allValues;
    for (NSString *key in keyArray) {
        NSLog(@"%@ : %@",key,dictionary4[key]);
    }

    enumerator = dictionary4.keyEnumerator;
    NSString *key;
    while (key = [enumerator nextObject]) {
        NSLog(@"%@ : %@",key,dictionary4[key]);
    }
           

7.在字典中,不能够存在相同的key,能够存在相同的value

8.在字典中,健值对是无序的.

//如果字典的key重复了,后面的值就会覆盖前面的值
    NSDictionary *dictionary9 = @{@"name":@"yanshuxin",@"name":@"bu"};
    NSLog(@"d9%@",dictionary9);
           
可变字典

1.1.初始化方法

NSMutableDictionary *mDictionary1 = [NSMutableDictionary dictionary];
    NSMutableDictionary *mDictionary2 = [NSMutableDictionary dictionaryWithObject:@"name" forKey:@"yanshuxin"];
    NSMutableDictionary *mDictionary3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"name",@"yanshuxin",@"age",@21, nil];
    //没有新语法
    NSMutableDictionary *mDictionary4 = [NSMutableDictionary dictionaryWithDictionary:dictionary8];

    //自己独有的初始化方法
    NSMutableDictionary *mDictionary5 = [NSMutableDictionary dictionaryWithCapacity:10];

    NSMutableDictionary *mDictionary6 = [NSMutableDictionary dictionaryWithContentsOfFile:@""];
    NSMutableDictionary *mDictionary7 = [NSMutableDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:@""]];
           

2.操作

//增
    [mDictionary5 setValue:@"yanshuxin" forKey:@"name"];
    NSLog(@"a%@",mDictionary5);

    //改
    [mDictionary5 setValue:@"YANSHUXIN" forKey:@"name"];
    NSLog(@"b%@",mDictionary5);

    //删
    [mDictionary5 removeObjectForKey:@"name"];
    [mDictionary5 removeObjectsForKeys:@[]];
    [mDictionary5 removeAllObjects];
    NSLog(@"c%@",mDictionary5);

    //新语法
    mDictionary5[@"name"] = @"carmen";
    NSLog(@"c%@",mDictionary5);