1 // 2 // main.m 3 // NSDictionary 4 // 5 // Created by dingxiaowei on 13-5-15. 6 // Copyright (c) 2013年 dingxiaowei. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 #import "Student.h" 11 #pragma mark - 字典的创建 12 void dicCreate(){ 13 //NSDictionary一旦创建是不可变的 可变的是NSMutableDictionary 14 //创建一个键值对 15 NSDictionary *dic=[NSDictionary dictionaryWithObject:@"dingxiaowei" forKey:@"10141303"]; //只能放OC对象,不能放基本数据类型 注意:不能处理中文 16 //创建多个键值对(最常用的初始化方法) 17 dic=[NSDictionary dictionaryWithObjectsAndKeys:@"cl",@"10141301", 18 @"cey",@"10141302", 19 @"dxw",@"10141303", nil]; 20 //其他创建方法 21 NSArray *objs=[NSArray arrayWithObjects:@"v1",@"v2",@"v3", nil]; 22 NSArray *keys=[NSArray arrayWithObjects:@"k1",@"k2",@"k3", nil]; 23 dic=[NSDictionary dictionaryWithObjects:objs forKeys:keys]; 24 NSLog(@"字典是:%@",dic); 25 } 26 #pragma mark - 字典的用法 27 void dicUse(){ 28 //创建多个键值对(最常用的初始化方法) 29 NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys: 30 @"v1",@"k1", 31 @"cey",@"10141302", 32 @"dxw",@"10141303", nil]; 33 NSLog(@"count:%zi",[dic count]); 34 //由于NSDictionary是不可变的,所以一旦创建只能查询,不能更改 35 id obj=[dic objectForKey:@"k1"]; 36 NSLog(@"k1-%@",obj); 37 38 //将字典写入文件中 39 NSString *path=@"/Users/dingxiaowei/Desktop/dictionary.xml"; 40 if([dic writeToFile:path atomically:YES]){ 41 NSLog(@"字典写入到文件成功"); 42 } 43 //读取xml的字典文件 44 dic=[NSDictionary dictionaryWithContentsOfFile:path]; 45 NSLog(@"文件读取成功\ndic:%@",dic); 46 47 //返回所有的keys和values 48 NSArray *keys=[dic allKeys]; 49 NSArray *values=[dic allValues]; 50 NSLog(@"所有的keys是:%@\n所有的值是:%@",keys,values); 51 52 //查询一个value对于的多个key 53 dic=[NSDictionary dictionaryWithObjectsAndKeys:@"v1",@"k1",@"v2",@"k2",@"v1",@"k3",nil]; 54 keys=[dic allKeysForObject:@"v1"]; 55 NSLog(@"keys:%@",keys); 56 57 //根据多个key取出对于的value 58 //注意:当key找不到对于的value时候,就用marker来代替 59 NSArray *array1=[NSArray array]; 60 array1=[dic objectsForKeys:[NSArray arrayWithObjects:@"k1",@"k2", @"k4",nil] notFoundMarker:@"not-found"];//如果找不到对于的object 就用marker代替 比如这儿没有k4 61 NSLog(@"找到的值%@",array1); 62 } 63 #pragma mark - 遍历字典 64 void dicFor(){ 65 //初始化字典 66 NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:@"v1",@"k1",@"v2",@"k2",@"v3",@"k3", nil]; 67 //遍历字典所有的key 68 for (id key in dic) { 69 id value=[dic objectForKey:key]; 70 NSLog(@"普通遍历:%@-%@",key,value); 71 } 72 73 //key迭代器遍历 74 NSEnumerator *enumer=[dic keyEnumerator]; 75 id key=nil; 76 while (key=[enumer nextObject]) { 77 id value=[dic objectForKey:key]; 78 NSLog(@"key迭代器遍历:%@-%@",key,value); 79 } 80 81 //value迭代器 82 //对象迭代器 83 //[dic objectEnumerator]; 84 85 //lblock遍历(每调遍历一次键值就调用一次block 86 [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { 87 NSLog(@"block迭代器遍历:%@-%@",key,obj); 88 }]; 89 } 90 #pragma mark - 字典的内存管理 91 void dicMemory(){ 92 Student *stu1=[Student studentWithName:@"cl"]; 93 Student *stu2=[Student studentWithName:@"cey"]; 94 Student *stu3=[Student studentWithName:@"dxw"]; 95 //字典会自动销毁,一般静态创建的对象都不需要我们自己释放,自动释放的时候会对key和value对象进行一次release操作 96 NSDictionary *dic=[NSDictionary dictionaryWithObjectsAndKeys:stu1,@"k1",stu2,@"k2",stu3,@"k3",nil]; //注意:把一个对象塞给字典时会进行一次retain时候 97 /*[stu1 retain]; 98 [stu2 retain]; 99 [stu3 retain];*/ 100 101 102 NSLog(@"dic%@",dic); 103 } 104 105 int main(int argc, const char * argv[]) 106 { 107 108 @autoreleasepool { 109 110 //dicCreate(); 111 //dicUse(); 112 //dicFor(); 113 dicMemory(); 114 } 115 return 0; 116 }
Student.h
1 #import <Foundation/Foundation.h> 2 3 @interface Student : NSObject 4 @property(nonatomic,retain)NSString *name; 5 +(id)studentWithName:(NSString *)name; 6 @end
Student.m
1 #import "Student.h" 2 3 @implementation Student 4 5 6 +(id)studentWithName:(NSString *)name{ 7 Student *stu=[[[Student alloc] init] autorelease]; 8 stu.name=name; 9 return stu; 10 } 11 12 -(NSString *)description{ 13 return _name; 14 } 15 16 -(void)dealloc{ 17 NSLog(@"名字为%@被销毁了",_name); 18 [_name release]; 19 [super dealloc]; 20 } 21 22 @end
时间: 2024-11-10 08:00:50