美文网首页
NSDictionary(字典)、NSMutableDictio

NSDictionary(字典)、NSMutableDictio

作者: 码农小白 | 来源:发表于2017-03-10 14:29 被阅读78次

    字典用于保存具有映射关系数据的集合
    一个key—value对认为是一个条目(entry),字典是存储key—value对的容器
    与数组不同,字典靠key存取元素
    key不能重复,value必须是对象
    键值对在字典中是无序存储的
    字典分:不可变字典(NSDictionary)和可变字典(NSMutableDictionary)
    不可变字典一旦创建,键值对就不可更改,不可添加,不可删除,仅能读取key或者value
    常用方法有:
    1、创建字典对象
    2、获取所有key值,获取所有value值
    3、通过key值查询value

    常见字典的常用方法

    在创建字典对象时需要赋值键值对,但是顺序为:值,键,(值在前键在后的形式

    NSDictionary *dic1 = [[NSDictionary alloc] initWithObjectsAndKeys:@"Duke",@"name",@33,@"age",@"男",@"gender", nil];
    NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Duke",@"name",@33,@"age",@"男",@"gender", nil];
    NSArray *keys = @[@"name",@"age",@"gender"];
    NSArray *values = @[@"Duke",@33,@"male"];
    

    字典的语法糖形式

    键值对之间以逗号隔开,键和值之间以冒号隔开

    NSDictionary *dic5 = @{@"name" : @"Duke",@"age" : @33,@"gender" : @"male"};
    

    创建字典对象时两个数组元素个数必须一致

    NSDictionary *dic3 = [[NSDictionary alloc] initWithObjects:values forKeys:keys];
    NSDictionary *dic4 = [NSDictionary dictionaryWithObjects:values forKeys:keys];
    

    通过count方法获取字典中键值对的个数:

    NSInteger count = [dic4 count];
    

    获取字典中所有的键:

    NSArray *allKeys = [dic4 allKeys];
    

    获取字典中所有的值组成的值:

    NSArray *allValues = [dic4 allValues];
    

    通过指定的键获取其在字典中对应的值:

    id object = [dic4 objectForKey:@"age"];
    for (int i = 0; i < count; i++) {
            id key = [allKeys objectAtIndex:i];
            //根据当前遍历得到的key去获取对应的value
            id value = [dic4 objectForKey:key];
            NSString *result = [value isKindOfClass:[NSString class]] ? @"YES" : @"NO";
            NSLog(@"%@:%@-->%@",key,value,result);
        }
    

    NSMutableDictionary可变字典

    可变NSMutableDictionary是NSDictionary的子类,拥有所有NSDictionary的方法
    常用方法有:
    1、创建字典对象
    2、添加键值对
    3、修改key对应的value
    4、删除键值对
    5、通过fou循环遍历所有键值对

    NSMutableDictionary可变字典对象的创建

        NSMutableDictionary *dic6 = [[NSMutableDictionary alloc] initWithDictionary:dic5];
        NSLog(@"%@",dic6);
        NSMutableDictionary *dic7 = [NSMutableDictionary dictionaryWithDictionary:dic5];
        NSLog(@"%@",dic7);
        NSMutableDictionary *dic8 = [[NSMutableDictionary alloc] init];
        NSLog(@"%@",dic8);
        NSMutableDictionary *dic9 = [NSMutableDictionary dictionary];
        NSLog(@"%@",dic9);
    

    增加键值对:

    [dic9 setObject:@"Duke" forKey:@"name"];
    [dic9 setObject:@"male" forKey:@"gender"];
    [dic9 setObject:@33 forKey:@"age"];
    

    修改已有键对应的值
    如果键不存在,则为添加键值对,如果键存在,则为修改已有键对应的值:

    [dic9 setObject:@34 forKey:@"age"];
    

    根据指定键去删除对应的键值对:

    [dic9 removeObjectForKey:@"age"];
    

    删除所有的键值对:

    [dic9 removeAllObjects];
    

    NSSet集合

    OC中的集合(NSSet)与数学中的集合一样,集合中的元素具有唯一性、存储单元的元素是无序的、存储元素必须是对象类型
    OC中用Set表示集合,分为NSSet和NSMutableSet
    NSSet对象的创建 无序性,互异性
    NSSet的常用方法
    1、创建集合对象
    2、获取元素个数
    3、获取集合中的某个元素
    4、判断集合中是否包含某个对象
    创建集合对象

    NSSet *set1 = [[NSSet alloc] initWithObjects:@"1",@"2",@"2",@"3", nil];
    NSLog(@"%@",set1);
    NSSet *set2 = [NSSet setWithObjects:@"3",@"2",@"1", nil];
    NSLog(@"%@",set2);
    

    用数组对象来创建集合对象
    可以通过这种方法过滤掉数组中重复的元素对象

    NSArray *array = @[@1,@2,@3,@2,@3];
    NSSet *set3 = [[NSSet alloc] initWithArray:array];
    NSSet *set4 = [NSSet setWithArray:array];
    

    获取集合中对象的个数:

    NSInteger count2 = [set4 count];
    

    获取集合中的对象:

    id object1 = [set4 anyObject];
    

    判断一个给定的对象是否包含在指定的集合中:

    NSString *result1 = [set4 containsObject:@3] ? @"YES" : @"NO";
    NSLog(@"%@is contained in set %@",@3,result1);
    

    NSMutableSet

    常用方法
    1、创建集合对象
    2、添加元素
    3、删除元素
    创建NSMutableset对象:

    NSMutableSet *mutableSet1 = [[NSMutableSet alloc] init];
    NSLog(@"%@",mutableSet1);
    NSMutableSet *mutableSet2 = [NSMutableSet set];
    NSLog(@"%@",mutableSet2);
    

    通过不可变对象创建

    NSMutableSet *mutableSet3 = [[NSMutableSet alloc] initWithSet:set1];
    NSLog(@"%@",mutableSet3);
    NSMutableSet *mutableSet4 = [NSMutableSet setWithSet:set1];
    NSLog(@"%@",mutableSet4);
    

    添加集合元素

    [mutableSet4 addObject:@4];
    

    删除单个集合

    [mutableSet4 removeObject:@"3"];
    

    删除所有元素

    [mutableSet4 removeAllObjects];
    

    NSCountedSet

    NSCountedSet是NSMutableString的子类
    能记录元素的重复次数
    在set的基础上添加了计数功能
    NSCountedSet记录添加进去的集合对象出现的次数

    NSCountedSet *countedSet = [NSCountedSet set];
    [countedSet addObject:@1];
    [countedSet addObject:@2];
    [countedSet addObject:@2];
    

    单独获取某个对象在集合中出现过多少次

    NSInteger countOfObjec = [countedSet countForObject:@1];
    

    通过快速枚举来遍历数组元素

    NSArray *testArray = @[@1,@2,@3,@4,@5];
    for (id object in testArray) {
         NSLog(@"%@",object);
    }
    for (NSNumber *object in testArray) {
         NSLog(@"%@",object);
    }
    //快速遍历集合
    for (id object in set1) {
         NSLog(@"%@",object);
    }
    

    快速遍历字典
    直接遍历字典直接得到的是字典的每一个键,可以通过遍历得到的键去获取对应的值

    for (NSString *key in dic1) {
         NSLog(@"dictionary[%@]:%@",key,dic1[key]);
    }
    //    dic1[key]就相当于[dic1 objectForKey:key]
    

    相关文章

      网友评论

          本文标题:NSDictionary(字典)、NSMutableDictio

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