美文网首页ios开发IOS
iOS-NSDictionary and NSMutableDi

iOS-NSDictionary and NSMutableDi

作者: 亦晴工作室 | 来源:发表于2016-08-24 14:55 被阅读2026次

    1.NSDictionary初始化

    1)字典中元素是键值对,所以字典元素都是成对出现的,一个元素就是一对(有key和对应的value)
    2)键和值都是对象类型,键和值的类型可以不同
    3)在字典中,不能出现相同的key,但可以出现相同的value,key的唯一性,这样就能够通过key唯一确定value

    //NSDictionary创建

             //不可变字典:字典对象一旦创建完成后,字典成员不能够被修改,增加和删除
        NSDictionary *dictionary = @{@"key1":@"value1", @"key2":@"value2"};
        //常用//初始化不可变字典//字典中的元素是成对出现的,要有key和value//key:value
        #pragma mark 对象方法创建字典
        NSDictionary *dictionary1 = [[NSDictionary alloc] initWithDictionary:dictionary];
        //用其他字典初始化当前的字典对象
        //字典中的key和value可以是任意的oc类型//nil:字典的结束标志
        NSDictionary *dictionary2 = [[NSDictionary alloc] initWithObjectsAndKeys:@"value1", @"key1",
                                @"value2", @"key2", nil];
        //value在前key在后,成对出现//最常用
        #pragma mark 类方法创建字典
        NSDictionary *dictionary3 = [NSDictionary dictionaryWithDictionary:dictionary];
        NSDictionary *dictionary4 = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1",
                                     @"value2", @"key2", nil];
    
    

    2.NSDictionary的操作
    //字典中元素个数

        NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"value1", @"key1",
                                      @"value2", @"key2", nil];
        //- (NSUInteger)count;
        //作用:字典中元素个数
        [dictionary count];
    

    //根据key(索引)找value(值)

        //- (id)objectForKey:(id)aKey;
        //作用:根据key找对应value
        [dictionary objectForKey:@"key1"];
    

    //获取字典中的所有的key

        //- (NSArray *)allKeys;
        //作用:获取字典中所有的key
        //返回是一个数组
        [dictionary allKeys];
    

    //获取字典中所有的value

        //第一种写法
        for (NSString *key in [dictionary allKeys]) {
            [dictionary objectForKey:key];
        }
        //第二种写法
        - (NSArray *)allValues;
    
    

    //获取某个值value对应的所有键key

        - (NSArray *)allKeysForObject:(id)anObject;
    
    

    3.NSMutableDictionary

    //可变字典初始化
        NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc] init];//创建一个空
             //对象方法初始化一个空的字典
        NSMutableDictionary *mutableDictionary1 = [[NSMutableDictionary alloc] initWithCapacity:0];
        //类方法初始化一个空的字典
        NSMutableDictionary *mutableDictionary2 = [NSMutableDictionary dictionaryWithCapacity:0];
    

    //向字典中添加元素(增加操作)

        NSDictionary *dic = @{@"height": @"183"};
        
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:0];//创建一个空字典
        //- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
        //作用:字典的末尾追加元素
        [dictionary setObject:@"chen chao" forKey:@"name"];
        [dictionary setObject:@"good teacher" forKey:@"info"];
        
        //- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;
        //作用:字典末尾追加一个字典
        [dictionary addEntriesFromDictionary:dic];
    
    //
    

    //字典中元素的删除(删除操作)

        //创建一个空字典
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:0];
       
        [dictionary setObject:@"chen chao" forKey:@"name"];
        [dictionary setObject:@"good teacher" forKey:@"info"];
        
        //- (void)removeObjectForKey:(id)aKey;
        //作用:根据key删除对应的元素
        [dictionary removeObjectForKey:@"info"];//常用
        
        //删除所有的元素
        [dictionary removeAllObjects];
    
    

    //字典中元素的修改

        NSDictionary *dic = @{@"height": @"183"};
        
        NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:0];//创建一个空字典
        [dictionary setObject:@"chen chao" forKey:@"name"];
        [dictionary setObject:@"good teacher" forKey:@"info"];
        
        //- (void)setObject:(id)anObject forKey:(id <NSCopying>)aKey;
        //作用:key值字典中已经存在,进行覆盖,如果不存在,追加
        [dictionary setObject:@"hot man" forKey:@"info"];
        
        //- (void)setDictionary:(NSDictionary *)otherDictionary;
        //作用:替换字典
        [dictionary setDictionary:dic];
    

    相关文章

      网友评论

        本文标题:iOS-NSDictionary and NSMutableDi

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