iOS中NSDictionary和NSMutableDictionary的用法

 

iOS中NSDictionary和NSMutableDictionary的用法 

标签: iosdictionary
  3758人阅读  评论(0)  收藏  举报
  分类:
[cpp]  view plain copy
  1. //  
  2. //  main.m  
  3. //  DictonaryTest  
  4. //  
  5. //  Created by Unisk on 13-9-19.  
  6. //  Copyright (c) 2013年 Test. All rights reserved.  
  7. //  
  8.   
  9. #import <Foundation/Foundation.h>  
  10. #import "Student.h"  
  11.   
  12. #pragma mark 字典的创建  
  13. void createDictionary(){  
  14.     //Dictionary是不可变的  
  15.     NSDictionary *dict=[NSDictionary dictionaryWithObject:@"V" forKey:@"K"];  
  16.     //  
  17.     dict=[NSDictionary dictionaryWithObjectsAndKeys:@"V1",@"k1",@"V2",@"k2",@"V3",@"k3", nil];  
  18.       
  19.     //  
  20.     //    NSArray *object=[NSArray arrayWithObjects:@"V1",@"V2",@"V3", nil];  
  21.     //    NSArray *key=[NSArray arrayWithObjects:@"G1",@"G2",@"G3", nil];  
  22.     //    dict=[NSDictionary dictionaryWithObjects:object forKeys:key];  
  23.     //  
  24.     //    NSLog(@"%zi",dict.count);  
  25.     //    NSLog(@"%@",dict);  
  26.       
  27.     // 从字典中取值  
  28.     //    id value=[dict objectForKey:@"k2"];  
  29.     //    NSLog(@"%@",value);  
  30.       
  31.     // 将字典中数据写入文件  
  32.     //    NSString *path=@"/Users/fdf/Desktop/dict.xml";  
  33.     //    [dict writeToFile:path atomically:YES];  
  34.       
  35.     //将文件中的数据读取到字典之中  
  36.     NSString *path=@"/Users/fdf/Desktop/dict.xml";  
  37.     id dict1=[NSDictionary dictionaryWithContentsOfFile:path];  
  38.     NSLog(@"%@",dict1);  
  39. }  
  40.   
  41. #pragma mark 字典的查询  
  42. void dictionaryFor(){  
  43.       
  44.     NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:@"V1",@"k1",@"V2",@"k2",@"V3",@"k3", nil];  
  45.     //   NSArray *array=[dict allKeys];  
  46.     //    NSArray *value=[dict allValues];  
  47.     // 传入多个key去查询value  
  48.     NSArray *value1= [dict objectsForKeys:[NSArray arrayWithObjects:@"k1",@"k2",@"k4", nil] notFoundMarker:@"not"];  
  49.     NSLog(@"%@ ",value1);  
  50.       
  51. }  
  52.   
  53. #pragma mark 字典的遍历  
  54. void dictionaryIterator(){  
  55.       
  56.     NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:@"V1",@"k1",@"V2",@"k2",@"V3",@"k3", nil];  
  57.     //快速遍历  
  58.     //    for (id key in dict) {  
  59.     //        id value=[ dict objectForKey:key];  
  60.     //        NSLog(@"%@=%@",key,value);  
  61.     //    }  
  62.     // while遍历  
  63.     //    NSEnumerator   *enums=[dict keyEnumerator];  
  64.     //    id key=nil;  
  65.     //    while (key=[enums nextObject]) {  
  66.     //        NSLog(@"%@==%@",key,[dict objectForKey:key]);  
  67.     //    }  
  68.     //block遍历  
  69.     [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {  
  70.         NSLog(@"%@==%@",key,obj);  
  71.           
  72.         if ([key isEqualTo:@"k2"]) {//停止循环  
  73.             *stop=YES;  
  74.         }  
  75.           
  76.     }];  
  77.       
  78. }  
  79.   
  80. #pragma mark 内存管理  
  81. void dictionaryMemory(){  
  82.     NSMutableDictionary *dict=[NSMutableDictionary dictionary];  
  83.     Student *stu1=[Student studentWithName:@"stu1"];  
  84.     [dict setObject:stu1 forKey:@"k"];  
  85.     [dict removeObjectForKey:@"k"];  
  86.      NSLog(@"%zi",[ stu1 retainCount]);  
  87.       
  88.       
  89.       
  90.     //    Student *stu2=[Student studentWithName:@"stu2"];  
  91.     //    Student *stu3=[Student studentWithName:@"stu3"];  
  92.       
  93.     //    NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:stu1,@"k1",stu2,@"k2",stu3,@"k3", nil];  
  94.     //    NSMutableDictionary *dict=[NSMutableDictionary dictionaryWithObjectsAndKeys:stu1,@"k1",stu2,@"k2",stu3,@"k3", nil];  
  95.       
  96.     //    NSLog(@"%zi",[ stu1 retainCount]);  
  97.       
  98.     //    [dict removeAllObjects];  
  99.     //    [dict removeObjectForKey:@"k2"];  
  100.     //    [dict release];  
  101.     //    [dict removeAllObjects];  
  102.     //    NSLog(@"%zi",[ stu1 retainCount]);  
  103.       
  104.       
  105. }  
  106.   
  107.   
  108.   
  109. int main(int argc, const char * argv[])  
  110. {  
  111.       
  112.     @autoreleasepool {  
  113.         dictionaryMemory();  
  114.     }  
  115.     return 0;  
  116. }  

[csharp]  view plain copy
  1. #import <Foundation/Foundation.h>  
  2.   
  3. @interface Student : NSObject  
  4.   
  5. @property (nonatomic,retain) NSString *name;  
  6.   
  7. +(id)studentWithName:(NSString *)name;  
  8.   
  9. @end  

[plain]  view plain copy
  1. #import "Student.h"  
  2.   
  3. @implementation Student  
  4.   
  5. #pragma mark 构造方法  
  6. +(id)studentWithName:(NSString *)name{  
  7.     Student *stu= [[[Student alloc] init] autorelease];  
  8.     stu.name=name;  
  9.     return  stu ;  
  10. }  
  11.   
  12. #pragma mark 自动释放内存  
  13. -(void)dealloc{  
  14.     NSLog(@"%@ 被销毁了",_name);  
  15.     [_name release];  
  16.     [super dealloc];  
  17. }  
  18.   
  19. @end  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值