Objective-c 字典对象
oc 中的 NSDictionary 的作用同 java 中的字典类相同,提供了 “键-值”对的组合。比如,是用字典类实现对学生姓名和学号的存放,编号是一个键(唯一性),姓名是值。它的方法有:
下面通过例子来具体说明它的用法:
1)构建字典
1 #import <Foundation/Foundation.h> 2 3 int main(int argc , const char *argv[]){ 4 @autoreleasepool{ 5 NSDictionary *dicti1 = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangwu",@"3", nil]; 6 NSEnumerator *keynum = [dicti1 keyEnumerator]; 7 for(NSString *key in keynum) 8 NSLog(@"key = %@ , value = %@",key,[dicti1 objectForKey:key]); 9 10 NSLog(@"----------------------------"); 11 NSDictionary *dicti2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangwu",@"3", nil]; 12 keynum = [dicti2 keyEnumerator]; 13 for(NSString *key in keynum) 14 NSLog(@"key = %@ , value = %@",key,[dicti2 objectForKey:key]); 15 16 NSLog(@"----------------------------"); 17 NSDictionary *dicti3 = @{@"1":@"zhangsan",@"2":@"lisi",@"3":@"wangwu"}; 18 keynum = [dicti3 keyEnumerator]; 19 for(NSString *key in keynum) 20 NSLog(@"key = %@ , value = %@",key,[dicti3 objectForKey:key]); 21 22 NSLog(@"------------------------------"); 23 NSArray *keyarray = @[@"1",@"2",@"3"]; 24 NSArray *valuearray = @[@"zhangsan",@"lisi",@"wangwu"]; 25 NSDictionary *dicti4 = [NSDictionary dictionaryWithObjects:valuearray forKeys:keyarray]; 26 keynum = [dicti4 keyEnumerator]; 27 for(NSString *key in keynum) 28 NSLog(@"key = %@ , value = %@",key,[dicti4 objectForKey:key]); 29 30 } 31 return 0; 32 }
2)字典的遍历
1 #import <Foundation/Foundation.h> 2 3 int main(int argc , const char *argv[]){ 4 @autoreleasepool { 5 NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangwu",@"3", nil]; 6 NSEnumerator *number = [dicti keyEnumerator]; 7 for(NSString *key in number) 8 NSLog(@"key = %@ , value = %@",key,[dicti objectForKey:key]); 9 10 NSLog(@"----------------------------"); 11 NSArray *keyarray = [dicti allKeys]; 12 for(NSString *key in keyarray) 13 NSLog(@"key = %@ , value = %@",key,[dicti objectForKey:key]); 14 15 NSLog(@"-----------------------------"); 16 for(NSString *key in dicti) 17 NSLog(@"key = %@ , value = %@",key,[dicti objectForKey:key]); 18 19 NSLog(@"-----------------------------"); 20 [dicti enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 21 NSLog(@"key = %@ ,value = %@",key,obj); 22 }]; 23 }
return 0; 24 }
3) 文件的读取
1 #import <Foundation/Foundation.h> 2 3 int main(int argc , const char *argv[]){ 4 @autoreleasepool { 5 NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangnwu",@"3",nil]; 6 7 NSString *filename = @"dicti.txt"; 8 BOOL iswrite = [dicti writeToFile:filename atomically:YES]; 9 if(iswrite) 10 NSLog(@"write to file ok"); 11 else 12 NSLog(@"write to file error"); 13 14 NSDictionary *dicti2 = [NSDictionary dictionaryWithContentsOfFile:filename]; 15 NSLog(@"dicti2 = %@",dicti2); 16 17 }
return 0; 18 }
4) 查找
1 #import <Foundation/Foundation.h> 2 3 int main(int argc , const char *argv[]) 4 { 5 @autoreleasepool { 6 NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangwu",@"3",@"zhangsan",@"4",nil]; 7 8 NSArray *keyarray = [dicti allKeys]; // 查找所有键值 9 NSLog(@"%@",keyarray); 10 11 NSArray *valuearray = [dicti allValues]; // 查找所有值 12 NSLog(@"%@",valuearray); 13 14 NSString *value = [dicti objectForKey:@"1"]; // 按键值查找 15 NSLog(@"value = %@",value); 16 17 value = dicti[@"2"]; 18 NSLog(@"value = %@",value); 19 20 NSArray *keyarray2 = @[@"1",@"2",@"3"]; // 利用数组的方式查找 21 NSArray *vlauearray2 = [dicti objectsForKeys:keyarray2 notFoundMarker:@"nil"]; 22 NSLog(@"vlauearray2 = %@",vlauearray2); 23 24 NSArray *keyarray3 = [dicti allKeysForObject:@"zhangsan"]; 25 NSLog(@"keyarray3 = %@",keyarray3); 26 27 NSDictionary *dicti2 = [dicti dictionaryWithValuesForKeys:keyarray2]; 28 NSLog(@"dicti2 = %@",dicti2); 29 } 30 return 0; 31 }
5) 排序
1 #import <Foundation/Foundation.h> 2 3 4 int main(int argc,char **argv){ 5 @autoreleasepool { 6 NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"2",@"lisi",@"3",@"wangwu",@"1",@"zhangsan",@"4",nil]; 7 NSLog(@"dicti = %@",dicti); 8 9 NSDictionary *dicti2 = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@1,@"lisi",@2,@"wangu",@3, nil]; 10 NSLog(@"dicti2 = %@",dicti2); 11 12 NSArray *array = [dicti2 keysSortedByValueUsingSelector:@selector(compare:)]; 13 for(NSNumber *key in array) 14 NSLog(@"key = %@,value = %@",key,dicti2[key]); 15 } 16 return 0; 17 }
2. 同样字典对象中也存在可变字典(NSDictionary),下面我们来通过一个例子来说明它的用法:
代码举例:
1 #import <Foundation/Foundation.h> 2 3 4 int main(int argc,char **argv){ 5 @autoreleasepool { 6 NSMutableDictionary *mdicti = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"1",@"lisi",@"2",@"wangwu",@"3",nil]; 7 8 [mdicti setValue:@"zhaoliu" forKey:@"4"]; // 如果 key 不存在就添加,key 存在就修改 9 NSLog(@"mdicti = %@",mdicti); 10 11 [mdicti setValue:@"zzz" forKey:@"1"]; 12 NSLog(@"mdicti = %@",mdicti); 13 14 mdicti[@"5"] = @"bbb"; 15 NSLog(@"mdicti = %@",mdicti); 16 17 NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"aaa",@"11",@"bbb",@"12",@"ccc",@"13",nil]; 18 [mdicti addEntriesFromDictionary:dicti]; 19 NSLog(@"dicti = %@",mdicti); 20 21 [mdicti removeObjectForKey:@"1"]; // 删除 22 NSLog(@"mdicti = %@",mdicti); 23 24 NSArray *array = @[@"11",@"12",@"13"]; 25 [mdicti removeObjectsForKeys:array]; 26 NSLog(@"mdicti = %@",mdicti); 27 28 [mdicti removeAllObjects]; // 删除所有的 29 NSLog(@"%@",mdicti); 30 } 31 return 0; 32 }