美文网首页
NSDictionary

NSDictionary

作者: 我不白先生 | 来源:发表于2020-09-28 14:17 被阅读0次

NSDictionary
1.1由键值对组成
1.1.1键(key):关键字。一般由字符串组成,不能重复
1.1.2值(object或value):字典中的数据。任意类的对象,可以重复
1.2创建方法
1.3求键值对个数
1.4字典与数组互相转换
1.5通过关键字找对应的值
1.6通过值找对应的关键字
1.7遍历
1.8对字典中的值进行排序的方法

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //创建方法
    NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3", nil];//带参初始化
    [self print:dict1];
    NSDictionary *dict2 = @{@"1":@"one",@"2":@"two",@"3":@"three"};//优化方法
    [self print:dict2];
    NSDictionary *dict3 = [NSDictionary dictionaryWithDictionary:dict2];
    [self print:dict3];
    //求键值对个数
    [self print:[NSNumber numberWithUnsignedLong:dict3.count]];
    //字典与数组互相转换
    //数组转字典
    NSArray *keys = @[@"1",@"2",@"3"];
    NSArray *objects = @[@"one",@"two",@"three"];
    NSDictionary *dict4 = [NSDictionary dictionaryWithObject:objects forKey:keys];
    [self print:dict4];
    //字典转数组
    keys = [dict3 allKeys];
    objects = [dict3 allValues];
    //通过关键字找对应的值
    NSString *str = [dict3 objectForKey:@"1"];//标准方法
    [self print:str];
    [self print:dict3[@"1"]];//优化方法
    keys = @[@"1",@"4"];
    objects = [dict3 objectsForKeys:keys notFoundMarker:@"该关键字在字典中不存在"];
    //[self print:objects];
    NSMutableString *str1 = [NSMutableString stringWithCapacity:20];
    for (NSString *s in objects)
    {
        [str1 appendFormat:@"%@\n",s];
    }
    [self print:str1];
    //通过值找对应的关键字
    keys = [dict3 allKeysForObject:@"two"];
    [self print:keys];
    //字典遍历
    str1 = [NSMutableString stringWithCapacity:10 * 3];
    for (NSString * key in dict3)
    {
        [str1 appendFormat:@"%@-%@\n",key,dict3[key]];
    }
    self.outputLabel.text = str1;
    //对字典中的值进行排序的方法
    TRStudent *stu1 = [TRStudent studentWithName:@"张三" andAge:18];
    TRStudent *stu2 = [TRStudent studentWithName:@"李四" andAge:20];
    TRStudent *stu3 = [TRStudent studentWithName:@"王五" andAge:19];
    NSDictionary *stu = @{@"1":stu1,@"2":stu2,@"3":stu3};
    NSArray *sorted = [stu keysSortedByValueUsingSelector:@selector(compareAge:)];//数组排序与字典排序唯一区别是排序的方法名不一样
    str1 = [NSMutableString stringWithCapacity:15 * 3];
    for(NSString * key in sorted)
    {
        [str1 appendFormat:@"%@-%@\n",key,stu[key]];
    }
    [self print:str1]; 
}

2.NSMutableDictionaty
2.1可变字典,是NSDictionary的子类
2.2创建方法
2.3添加方法
2.4修改方法
2.5删除方法

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //创建方法
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];//空字典
    NSMutableDictionary *dict2 = [NSMutableDictionary dictionaryWithCapacity:100];//预估值
    NSMutableDictionary *dict3 = @{@"1":@"one",@"2":@"two",@"3":@"three"};//dict3将退化成NSDictionary
    NSMutableDictionary *dict4 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"one",@"1",@"two",@"2",@"three",@"3",nil];
    //添加方法
    [dict4 setValue:@"four" forKey:@"4"];
    [self print:dict4];
    NSDictionary *added = @{@"5":@"five",@"6":@"six",@"7":@"seven"};
    [dict4 addEntriesFromDictionary:added];
    [self print:dict4];
    //修改(赋值)方法
    NSDictionary *dict5 =@{@"1":@"one",@"2":@"two",@"3":@"three"};
    NSMutableDictionary *dict6 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"aaa",@"1",@"bbb",@"2",@"ccc",@"3",nil];
    [self print:dict6];
    [dict6 setDictionary:dict5];
    [self print:dict6];
    //删除方法
    [dict4 removeObjectForKey:@"1"];//删除一个
    [self print:dict4];
    NSArray *del = @[@"3",@"5"];
    [dict4 removeObjectsForKeys:del];//删除多个
    [self print:dict4];
    [dict4 removeAllObjects];//清空字典
    self.outputLabel.text = [NSString stringWithFormat:@"%lu",dict4.count];
    ```

相关文章

网友评论

      本文标题:NSDictionary

      本文链接:https://www.haomeiwen.com/subject/jvfduktx.html