OC知识--Foundation框架详尽总结之『字典类』

作者: ITCharge | 来源:发表于2016-08-07 08:22 被阅读211次

    本文首发于我的个人博客:『不羁阁』 https://bujige.net
    文章链接:https://bujige.net/blog/iOS-Foundation-Dictionary.html

    本文对Foundation框架中的字典类(NSDictionary和NSMutableDictionary)的使用做一个详细的总结。

    1. NSDictionary

    1.NSDictionar介绍

    • Dictionary翻译过来叫做"字典"
    • 日常生活中,“字典”的作用:通过一个拼音或者汉字,就能找到对应的详细解释
    • NSDictionary的作用类似:通过一个键(key),就能找到对应的值(value)
    • NSDictionary中的键(key)是单值,通常是字符串,也可以是其他对象类型
    • NSDictionary中和键(key)关联的值(value)可以是任何对象类型,但不能是nil
    • NSDictionary是不可变的,一旦初始化完毕,里面的内容就无法修改

    2.NSDictionary的创建

    + (instancetype)dictionary;
    + (instancetype)dictionaryWithObject:(id)object forKey:(id <NSCopying>)key;
    + (instancetype)dictionaryWithObjectsAndKeys:(id)firstObject, ...;
    + (id)dictionaryWithContentsOfFile:(NSString *)path;
    + (id)dictionaryWithContentsOfURL:(NSURL *)url;
    

    3. NSDictionary创建和获取简写

    • 以前NSDictionary创建方式
    NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"Walkers", @"name", @"12345678", @"phone", @"ZhongGuo", @"address", nil];
    NSDictionary *dict = [NSDictionary dictionaryWithObjects:@[@"Walkers",@"30",@"1.75"] forKeys:@[@"name",@"age",@"height"]];
    
    
    • 现在NSDictionary创建简写方式
    NSDictionary *dict = @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"};
    
    • 以前NSDictionary获取元素方式
    [dict objectForKey:@"name”];
    
    • 现在NSDictionary获取元素简写方式
    dict[@"name”];
    

    4. 键值对集合的特点

    • 字典存储的时候,必须是"键值对"的方式来存储(同时键不要重复)
    • 键值对中存储的数据是"无序的"
    • 键值对集合可以根据键,快速获取数据

    5. NSDictionary的遍历

    • 返回字典的键值对数目- (NSUInteger)count;
    NSDictionary *dict = @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"};
    NSLog(@"count = %lu",[dict count]);
    
    输出结果:count = 3
    
    • 根据key取出value- (id)objectForKey:(id)aKey;
    NSDictionary *dict = @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"};
    NSLog(@"%@",[dict objectForKey:@"name"]);
    
    输出结果:Walkers
    
    
    • 快速遍历
    
    NSDictionary *dict = @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"};
    
    for (NSString *key in dict) {
        NSLog(@"key = %@, value = %@", key, dict[key]);
    }
    
    输出结果:
    key = name, value = Walkers
    key = phone, value = 12345678
    key = address, value = ZhongGuo
    
    • Block遍历
    [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
        NSLog(@"key = %@, value = %@", key, obj);
    }];
    

    6. NSDictionary文件操作

    • 将字典写入文件中
      • - (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
      • - (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;
      • 存储结果是xml文件格式,但苹果官方推荐为plist后缀
    • 示例
    
    NSDictionary *dict = @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"};
    
    BOOL flag = [dict writeToFile:@"/Users/Walkers/Desktop/dict.plist" atomically:YES];
    NSLog(@"flag = %i", flag);
    
    输出结果:flag = 1
    

    文件里的内容显示结果如下图

    1.png
    • 从文件中读取字典
    
    NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/Walkers/Desktop/dict.plist"];
    NSLog(@"newDict = %@", newDict);
    
    

    7. NSDictionary和NSArray对比

    1. NSDictionary和NSArray的区别

    • NSArray是有序的,NSDictionary是无序的
    • NSArray是通过下标访问元素,NSDictionary是通过key访问元素

    2. NSDictionary和NSArray的用法

    • 创建
    // 数组
    @[@"Walkers", @"Rose"] (返回是不可变数组)
    // 字典
    @{ @"name" : @"Walkers", @"phone" : @"12345678" } (返回是不可变字典)
    
    
    • 访问
    // 数组
    id d = array[1];
    // 字典
    id d = dict[@"name"];
    
    
    • 赋值
    // 数组
    array[1] = @"Walkers";
    // 字典
    dict[@"name"] = @"Walkers";
    
    

    2. NSMutableDictionary

    1.NSMutableDictionary介绍

    • NSMutableDictionary是NSDictionary的子类
    • NSDictionary是不可变的,一旦初始化完毕后,它里面的内容就永远是固定的,不能删除里面的元素,也不能再往里面添加元素
    • NSMutableDictionary是可变的,随时可以往里面添加\更改\删除元素

    2. NSMutableDictionary的常见操作

    • 添加/修改一个键值对(如果aKey之前有值,则会把aKey之前对应的值给替换掉)- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
    
    NSMutableDictionary *dict = [NSMutableDictionary  dictionary];
    
    [dict setObject:@"Walkers" forKey:@"name"];
    NSLog(@"%@", dict);
    
    输出结果:
    {
        name = Walkers;
    }
    
    NSMutableDictionary *dict = [NSMutableDictionary  dictionary];
    
    [dict setObject:@"Walkers" forKey:@"name"];
    NSLog(@"%@", dict);
    
    [dict setObject:@"abc" forKey:@"name"];
    NSLog(@"%@", dict);
    
    输出结果:
    {
        name = Walkers;
    }
    {
        name = abc;
    }
    
    • 通过aKey删除对应的value- (void)removeObjectForKey:(id)aKey;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValuesForKeysWithDictionary: @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"}];
    
    [dict removeObjectForKey:@"name"];
    NSLog(@"%@",dict);
    
    输出结果:
    {
        address = ZhongGuo;
        phone = 12345678;
    }
    
    • 删除所有的键值对- (void)removeAllObjects;
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setValuesForKeysWithDictionary: @{@"name":@"Walkers", @"phone":@"12345678", @"address":@"ZhongGuo"}];
    
    [dict removeAllObjects];
    NSLog(@"%@",dict);
    
    输出结果:
    {
    }
    

    3. NSMutableDictionary的简写

    • 以前设置键值对方式
    [dict setObject:@"Jack" forKey:@"name”];
    
    • 现在设置键值对方式
    dict[@"name"] = @"Jack";
    

    相关文章

      网友评论

        本文标题:OC知识--Foundation框架详尽总结之『字典类』

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