博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
oc75--不可变字典NSDictionary
阅读量:4615 次
发布时间:2019-06-09

本文共 2471 字,大约阅读时间需要 8 分钟。

////  main.m//  NSDictionary////#import 
int main(int argc, const char * argv[]) { // 1.如何创建 NSDictionary *dict1 = [NSDictionary dictionaryWithObject:@"lnj" forKey:@"name"]; NSString *name1 = [dict1 objectForKey:@"name"]; NSLog(@"name = %@", name1); //lnj NSLog(@"dict1 = %@", dict1); //dict1 = {name = lnj;} // 注意: key和value 是一一对应 NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:@[@"lnj", @"30", @"1.75"] forKeys:@[@"name", @"age", @"height"]]; NSLog(@"dict2 = %@", dict2); //{age = 30;height = "1.75";name = lnj;} NSLog(@"%@ %@ %@", [dict2 objectForKey:@"name"], [dict2 objectForKey:@"age"], [dict2 objectForKey:@"height"]); //NSDictionary *dict3 = @{key:value}; NSDictionary *dict4 = @{
@"name": @"lnj"}; NSLog(@"%@", dict4[@"name"]); NSDictionary *dict5 = @{
@"name":@"lnj", @"age":@"30", @"height":@"1.75"}; NSLog(@"dict5 = %@", dict5);//{age = 30;height = "1.75";name = lnj;} NSLog(@"%@ %@ %@", dict5[@"name"], dict5[@"age"], dict5[@"height"]); // 2.字典的遍历 NSDictionary *dict6 = @{
@"name":@"lnj", @"age":@"30", @"height":@"1.75"}; // 2.1如何获取字典中key和value的个数, 在字典中key称之为键, value称之为值 NSLog(@"count = %lu", [dict6 count]); for (int i = 0; i < dict6.count; ++i) { // 获取字典中所有的key NSArray *keys = [dict6 allKeys]; // 取出当前位置对应的key NSLog(@"%@", keys[i]); NSString *key = keys[i]; NSString *value = dict6[key]; NSLog(@"key = %@, value = %@", key, value); } // 如何通过forin遍历字典, 会将所有的key赋值给前面的obj for (NSString *key in dict6) { NSLog(@"%@", key); NSString *value = dict6[key]; NSLog(@"key = %@, value = %@", key, value); } [dict6 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { NSLog(@"key = %@, value = %@", key, obj); }]; // 3.字典文件读写 NSDictionary *dict7 = @{
@"name":@"lnj", @"age":@"30", @"height":@"1.75"}; // XML 扩展名plist [dict7 writeToFile:@"/Users/mctc/Desktop/a.plist" atomically:YES]; // 注意: 字典和数组不同, 字典中保存的数据是无序的 NSDictionary *newDict8 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/mctc/Desktop/a.plist"]; NSLog(@"%@", newDict8);// {age = 30;height = "1.75";name = lnj;} NSArray *arr9 = @[@10, @20, @30, @5]; [arr9 writeToFile:@"/Users/mctc/Desktop/a.plist" atomically:YES]; return 0;}

 

转载于:https://www.cnblogs.com/yaowen/p/7441030.html

你可能感兴趣的文章
数组加1问题
查看>>
ubuntu 12.04安装telnet和ssh服务
查看>>
Halcon一日一练:图像采集设备的基本参数
查看>>
WP7应用开发笔记(1) 序言
查看>>
Spring第五章:AOP(Schema-based)
查看>>
发现一些有趣的工具
查看>>
[翻译] UIView-draggable 可拖拽的UIView
查看>>
centos 下tomcat安装
查看>>
java基础-Reference三
查看>>
java登陆验证码与JS无刷新验证
查看>>
Spring3中js/css/jpg/gif等静态资源无法找到(No mapping found for HTTP request with URI)问题解决...
查看>>
POJ 2601
查看>>
android基本控件学习-----ToggleButton&Switch
查看>>
python采用pika库使用rabbitmq(五)消息持久化存储(Message durability)
查看>>
css禁止鼠标事件
查看>>
5.建造者模式
查看>>
matlab 函数的编写与调用
查看>>
jfinal相关
查看>>
Photoshop | 快速抠头发(调整边缘/选择并遮住)
查看>>
转载:EM算法的最精辟讲解
查看>>