NSDictionary/NSMutableDictionary

NSDictionary

由键值对组成,关键字不允许重复,值可以重复

1. 创建字典(4种)
//创建字典的标准方法
1、NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"one",@"1"/*先写值,后写关键字*/,@"two",@"2", nil];
//优化方法,最常用
2、NSDictionary *dic2 = @{@"1":@"one",@"2":@"two"};
//创建字典的副本
3、NSDictionary *dic4 = [NSDictionary dictionaryWithDictionary:dic3];
//将数组转换成字典,两个数组的关键字和值必须对应
4、NSArray *a1 = @[@"one",@"two",@"three"];
   NSArray *a2 = @[@"1",@"2",@"3"];
   NSDictionary *dic3 = [NSDictionary dictionaryWithObjects:a1 forKeys:a2];

2. 将字典中的所有值转换成数组的方法
//获取字典中的所有值的方法,即将字典转换成数组的方法
a1 = [dic3 allValues];
NSLog(@"%@",a1);
3. 将字典中的所有关键字转换成数组
//获取字典中的所有值的方法,即将字典转换成数组的方法
a1 = [dic3 allValues];

4. 通过关键字找到对应的值(3种)
//根据一个关键字查找一个值
1、 NSString *str = [dic3 objectForKey:@"1"];
//优化后的方法,最常用,类似数组
2、 str = dic3[@"1"];
 //根据多个关键字找多个值
3、NSArray *key1 = @[@"1",@"4"];
   NSArray *object1 = [dic3 objectsForKeys:key1 notFoundMarker:@"无对应值"];
5. 求键值对个数的方法
NSLog(@"%lu",dic3.count);//数组,集合,字典,元素个数都是count
6. 通过值找到对应的关键字
NSArray *key2 = [dic3 allKeysForObject:@"two"];//把two的所有关键字找出来
7. 对字典中的值进行排序
//对字典中的值进行排序
NSArray *key3 = [dic3 keysSortedByValueUsingSelector:@selector(compare:)];//进行排序

//自定义排序;
//根据年龄排序
        TRStudent *stu1 = [[TRStudent alloc]initWithName:@"张三" andAge:18];
        TRStudent *stu2 = [[TRStudent alloc]initWithName:@"张四" andAge:29];
        TRStudent *stu3 = [[TRStudent alloc]initWithName:@"张五" andAge:20];
        NSDictionary *stu = @{@"1":stu1,@"2":stu2,@"3":stu3};
        NSArray *sortedAge = [stu keysSortedByValueUsingSelector:@selector(compareAge:)];
        for (NSString *key in sortedAge){
            NSLog(@"%@ = %@",key,stu[key]);
        }

-(NSComparisonResult)compareAge:(TRStudent *)other{
    NSNumber *selfAge = [NSNumber numberWithInt:self.age];
    NSNumber *otherAge = [NSNumber numberWithInt:self.age];
    return [selfAge compare:otherAge];
}

8. 遍历(3种)
//枚举器值遍历
NSEnumerator *e = [dic3 objectEnumerator];
        while (str = [e nextObject]){
            NSLog(@"%@",str);
        }

//枚举器关键字遍历
        e = [dic3 keyEnumerator];
        while (str = [e nextObject]) {
            NSLog(@"%@",str);
        }

//只遍历关键字,不遍历值;快速遍历
        for (NSString *s in dic3){
            NSLog(@"%@ = %@",s,dic3[s]);
        
9. 字典的文件读写
//字典的文件读写
 [dic3 writeToFile:@"/Users/ios-33/Desktop/代码/第一阶段代码/Foundation/day23-8:30/day23-01字典/day23.rtf" atomically:YES];//写进文件
NSDictionary *dic5 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/ios-33/Desktop/代码/第一阶段代码/Foundation/day23-8:30/day23-01字典/day23.rtf"];//读取文件

NSMutableDictionary

可变字典,是不可变字典的子类

1. 创建方法(3种)
1、NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];//空字典,有意义,可以添加键值对
2、NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithCapacity:100];//预估值创建(如果超过预估值的话创建效率就会慢)
3、NSMutableDictionary *dict3 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];//存储在堆上;标准方法
2. 添加方法(2种)
1、[dict3 setValue:@"four" forKey:@"4"];
//添加多个键值对的方法
2、NSDictionary *added = @{@"5":@"five",@"6":@"six",@"7":@"seven"};
  [dict3 addEntriesFromDictionary:added];
3. 覆盖方法
NSDictionary *dict5 = @{@"1":@"one",@"2":@"two",@"3":@"three"};
//NSMutableDictionary *dict6 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"aaa",@"1",@"bbb",@"2",@"ccc",@"3", nil];
//[dict6 setDictionary:dict5];//dict5覆盖了dict6
4. 删除方法(3种)
1、[dict3 removeObjectForKey:@"1"];//删除指定关键字对应的键值对
2、NSArray *del = @[@"3",@"5"];//批量删除指定关键字对应的键值对
//[dict3 removeObjectsForKeys:del];
3、 [dict3 removeAllObjects];//清空字典

4、    NSMutableDictionary *dir = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];
    NSLog(@"%@",dir[@"2"]); //根据键来取对应的值
    NSLog(@"%@",dir[@"two"]); //null取不到
posted @ 2017-08-23 20:41  笑笑就好90  阅读(220)  评论(0编辑  收藏  举报