美文网首页
NSDictionary

NSDictionary

作者: 哔哩哔哩智能喵 | 来源:发表于2016-08-23 00:06 被阅读5次
           //字典都是一个key 对应一个 value
            NSDictionary *d = [NSDictionary dictionaryWithObject:@"lxc" forKey:@"name"];
            
            //字典的排列是无序排列,这点和数组不同数组是有序排列
            NSDictionary *d1 = [NSDictionary dictionaryWithObjects:@[@"lxc",@"23",@"178"] forKeys:@[@"name",@"age",@"height"]];
            
            //快速创建字典
            NSDictionary *dd = @{@"name":@"cyf"};
            NSLog(@"%@",dd);
            NSDictionary *d11 = @{@"name":@"cyf",@"age":@"22",@"height":@"155"};
            NSLog(@"%@",d11);
            //字典文件写入
            [d11 writeToFile:@"/Users/liuxingchen/Desktop/test.plist" atomically:YES];
            //字典文件读取
            NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:@"/Users/liuxingchen/Desktop/test.plist"];
            //用for i 遍历字典
            for (int i = 0; i< d1.count; ++i) {
                NSArray *keys = [d1 allKeys];
                NSString *key = keys[i];
                NSString *value = d1[key];
                NSLog(@"%@ = %@",key,value);
            }
            //用for in 遍历字典
            for (NSString *key in d11) {
                NSString *value = d11[key];
                NSLog(@"%@ = %@",key,value);
            }
            //用OC迭代器遍历字典
            [d11 enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
                NSLog(@"%@ = %@",key,obj);
            }];
    //字典写入
            [dict writeToFile:@"/Users/yao/Desktop/test.plist"atomically:YES];
            //字典读取
            NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:@"/Users/yao/Desktop/test.plist"];
        };
    

    相关文章

      网友评论

          本文标题:NSDictionary

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