美文网首页
OC语言day08-08不可变字典NSDictionary

OC语言day08-08不可变字典NSDictionary

作者: liyuhong165 | 来源:发表于2016-07-31 20:39 被阅读65次

    pragma mark 不可变字典NSDictionary

    pragma mark 概念

    /**
     NSDictionary
     1.创建
    字典_单个键值对 (dictionaryWithObject:forKey)
    字典_多个个键值对 (dictionaryWithObjects:forKeys)
    
     2.取值
     字典_单个值 获取  (objectForKey)  字典_单个值 快速获取 dict[@"value"];
     字典_多个键值对 @{@"v1":@"k1",@"v2",@"k2"}
     
     3. 获取字典的键值对 个数
     (dict.count)
     
     4.遍历字典
     4.1for
     
     for (int i = 0; i<dict.count; ++i) {
     // 获取 字典中所有的key
     NSArray *keys = [dict allKeys];
     // 取出当前位置 对应的key
     NSLog(@"%@",keys[i]);
     
     NSString *key = keys[i];
     NSString *value = dict[key];
     NSLog(@"key = %@, value = %@",key, value);
     
     }
     
     4.2 for in 
     // 如何通过 for in遍历字典 , 会将所有的key赋值 给前面的obj
     for (NSDictionary *key in dict) {
     NSString *value = dict[key];
     NSLog(@"key2 = %@, value2 = %@",key, value);
     }
     
     4.3 (enumerateKeysAndObjectsUsingBlock)
     [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
     NSLog(@"key3 = %@, value3 = %@",key, obj);
     
     }];
     */
    

    pragma mark 代码

    #import <Foundation/Foundation.h>
    #pragma mark 类
    
    #pragma mark main函数
    int main(int argc, const char * argv[])
    {
     
    #pragma mark 不可变字典NSDictionary
        
    #warning 1.如何创建 (dictionaryWithObject:forKey)
        // obj 值 \
            key 键
        NSDictionary *dict = [NSDictionary dictionaryWithObject:@"lyh" forKey:@"name"];
    #warning 1.1.取值 (objectForKey)
        NSString *name = [dict objectForKey:@"name"];
        NSLog(@"%@",name);
        
    #warning 2. 创建多个键值对
        // 注意: key 和 value 是一一对应的
        NSDictionary *dict1 = [NSDictionary dictionaryWithObjects:@[@"lyh",@"24",@"172"] forKeys:@[@"name",@"age",@"height"]];
        NSLog(@"%@ %@ %@",[dict1 objectForKey:@"name"],[dict1 objectForKey:@"age"],[dict1 objectForKey:@"height"]);
    
    #warning 3.简单创建 字典 @{键:值}
    //    NSDictionary *dict2 = @{key:value};
        NSDictionary *dict2 = @{@"name":@"lyh"};
        NSLog(@"%@",[dict2 objectForKey:@"name"]);
        NSLog(@"%@",dict2[@"name"]);
        
     
    #warning 4.创建多个 键值对 字典
        NSDictionary *dict3 = @{@"name":@"lyh",@"age":@"25",@"height":@"170"};
        NSLog(@"%@ %@ %@",dict3[@"name"],dict3[@"age"],dict3[@"height"]);
        
        
    #pragma mark 5.字典的遍历
    #warning 5.1 获取键值对
        dict = @{@"name":@"lys",@"age":@"28",@"height":@"171"};
        // 如何获取字典中 key 和 value的个数, 在字典中 key称之为 键, value称之为 值
        NSLog(@"count = %lu",dict.count);
     
        
    #warning 5.2 字典遍历
        // for
    #warning 5.2.1 字典遍历 (for)
        for (int i = 0; i<dict.count; ++i) {
            // 获取 字典中所有的key
            NSArray *keys = [dict allKeys];
            // 取出当前位置 对应的key
            NSLog(@"%@",keys[i]);
            
            NSString *key = keys[i];
            NSString *value = dict[key];
            NSLog(@"key = %@, value = %@",key, value);
            
        }
    #warning 5.2.2 字典遍历 (for in)
        // 如何通过 for in遍历字典 , 会将所有的key赋值 给前面的obj
        for (NSDictionary *key in dict) {
            NSString *value = dict[key];
        }
        
        
    #warning 5.2.3 字典遍历 (enumerateKeysAndObjectsUsingBlock)
    
        [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            NSLog(@"key3 = %@, value3 = %@",key, obj);
    
        }];
        
        
    #warning 6. 字典文件读写
        dict = @{@"name":@"lyh",@"age":@"34"};
        
        [dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            NSLog(@"key4 = %@, value = %@",key, obj);
            
        }];
        
        // XML 扩展名plist
        int flag = [dict writeToFile:@"/Users/liyuhong165/Desktop/info.plist" atomically:YES];
        NSLog(@"flag  = %d",flag);
        
        // 注意: 字典和数组 不同, 字典中保存的数 都是无序的
        NSDictionary *newDict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/liyuhong165/Desktop/info.plist"];
        NSLog(@"%@",newDict);
        
        
    #warning 7.数组是有序的 字典是无序的
        NSArray *arr = @[@10,@30,@50,@121];
        [arr writeToFile:@"/Users/liyuhong165/Desktop/info.plist" atomically:YES];
        return 0;
    }
    
    

    相关文章

      网友评论

          本文标题:OC语言day08-08不可变字典NSDictionary

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