介绍的api
//// main.m// NSDiconary//// Created by ganchaobo on 13-4-29.// Copyright (c) 2013年 ganchaobo. All rights reserved.//#importvoid 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;}