博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OBJEct-c中NSDictionary的用法
阅读量:6233 次
发布时间:2019-06-22

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

 介绍的api

 

////  main.m//  NSDiconary////  Created by ganchaobo on 13-4-29.//  Copyright (c) 2013年 ganchaobo. All rights reserved.//#import 
void CreateDic(){ //第一种创建字典,创建多个key-value NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"k1",@"v1" ,@"k2",@"v2",@"k3",@"v3", nil]; //NSLog(@"%@",dic); //第二种创建字典 创建一对 key-value NSDictionary *dic1=[NSDictionary dictionaryWithObject:@"VV" forKey:@"KK"]; // NSLog(@"%@",dic1); //第三种创建多对间值 NSArray *key=[NSArray arrayWithObjects:@"K1",@"K2",@"K3", nil]; NSArray *value=[NSArray arrayWithObjects:@"V1",@"V2",@"V3", nil]; NSDictionary *dic2=[NSDictionary dictionaryWithObjects:key forKeys:value]; //NSLog(@"%@",dic2); NSDictionary *dic3=[NSDictionary dictionaryWithDictionary:dic2]; NSLog(@"%@",dic3);}void DicMethod(){ NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"K1",@"V1",@"K2",@"V2",@"K3",@"V3", nil]; NSDictionary *dic1=[NSDictionary dictionaryWithObjectsAndKeys:@"K1",@"V1",@"K1",@"V2",@"K3",@"V3", nil]; NSLog(@"%zi",[dic count]);//字典种右多少对key-value NSLog(@"%@",[dic allKeys]);//字典种的所有keys NSLog(@"%@",[dic allValues]);//字典中所有的values NSLog(@"%i",[dic isEqualToDictionary:dic1]); NSLog(@"%@",[dic objectForKey:@"V1"]); NSArray *arr=[dic1 allKeysForObject:@"K1"];//通过value找对应多个key NSArray *arr1=[NSArray arrayWithObjects:@"V1",@"V2",@"VG", nil]; NSArray *ARR= [dic objectsForKeys:arr1 notFoundMarker:@"BB"]; NSLog(@"%@",ARR); }void WriteFile(){ NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"k1",@"v1",@"k2",@"v2", nil]; [dic writeToFile:@"/Users/ganchaobo/Desktop/2.txt" atomically:YES]; }void ReadFile(){ NSDictionary *dic=[NSDictionary dictionaryWithContentsOfFile:@"/Users/ganchaobo/Desktop/2.txt"]; NSLog(@"%@",dic);}//第一次遍历void bianli1(){ NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"k1",@"v1",@"k2",@"v2", nil]; NSArray *arr=[dic allKeys]; for(id key in arr){ id obj=[dic objectForKey:key]; NSLog(@"Key-->%@ value--->%@",key,obj); }}//第二种遍历void bianli2(){ NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"k1",@"v1",@"k2",@"v2", nil]; NSEnumerator *e= [dic keyEnumerator]; id key=nil; while(key=[e nextObject]){ id obj=[dic objectForKey:key]; NSLog(@"Key-->%@ value--->%@",key,obj); }}//第三种遍历void bianli3(){ NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"k1",@"v1",@"k2",@"v2", nil]; [dic enumerateKeysAndObjectsUsingBlock:^(id key,id value,BOOL *stop){ NSLog(@"Key-->%@ value--->%@",key,value); }];}/key排序void sort1(){ NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"AC",@"3",@"AF",@"2",@"AE", nil]; NSArray *arr= [dic keysSortedByValueUsingSelector:@selector(compare:)]; //通过value排序,返回key集合 NSLog(@"%@",arr);}void sort2(){ NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"1",@"AC",@"3",@"AF",@"2",@"AE", nil]; NSLog(@"%@",dic);}//NSMustableDictionaryvoid CreateMutableDic(){ NSMutableDictionary *mutableArray=[NSMutableDictionary dictionaryWithObjectsAndKeys:@"V1",@"K1",@"V2",@"K2", nil]; NSMutableDictionary *dic=[NSMutableDictionary dictionaryWithObject:@"AA" forKey:@"BB"]; //[mutableArray addEntriesFromDictionary:dic];//在字典种添加字典 //[mutableArray removeAllObjects]; //[mutableArray removeObjectForKey:@"K1"];//通过key移除键值对 //[mutableArray setDictionary:dic];//重新设置字典 //[mutableArray setObject:@"VV" forKey:@"K1"];//通过字典的key设置value //[mutableArray setObject:[NSNull null] forKey:@"K1"];//通过key 设置value 为null NSArray *arr=[NSArray arrayWithObjects:@"K1",@"K3" , nil]; [mutableArray removeObjectsForKeys:arr];//删除数组中相同的key对应的key-value NSLog(@"%@",mutableArray);}int main(int argc, const char * argv[]){ @autoreleasepool { CreateMutableDic(); } return 0;}

 

 

转载地址:http://kgqna.baihongyu.com/

你可能感兴趣的文章
Android中AsyncTask的简单用法
查看>>
Android 获取dimen值
查看>>
Camel In Action 读书笔记 (9)
查看>>
Gallery实现首页图片滑动源码
查看>>
ehlib 修改 使指示区背景色 和 数据区 背景色一致
查看>>
关于MySQL的init-file选项的用法实例
查看>>
对memcached使用的总结和使用场景
查看>>
Python 当前时间增加或减少一个月
查看>>
CollectionUtil
查看>>
平衡二叉树
查看>>
Android应用开发新路线
查看>>
smartHost 北京服务器
查看>>
制作自己的网络字体
查看>>
Xcode的包管理器:Alcatraz
查看>>
WinForms Adorner UI Manager v16.1支持高亮特定控件
查看>>
开源 免费 java CMS - FreeCMS1.2-功能说明-会员管理
查看>>
apache的mime.types作用
查看>>
语言的对于处理器的字长问题
查看>>
Virgo IDE Milestones
查看>>
查询数据库布局信息
查看>>