分类:
版权声明:本文为博主原创文章,未经博主允许不得转载。
- //
- // main.m
- // DictonaryTest
- //
- // Created by Unisk on 13-9-19.
- // Copyright (c) 2013年 Test. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import "Student.h"
- #pragma mark 字典的创建
- void createDictionary(){
- //Dictionary是不可变的
- NSDictionary *dict=[NSDictionary dictionaryWithObject:@"V" forKey:@"K"];
- //
- dict=[NSDictionary dictionaryWithObjectsAndKeys:@"V1",@"k1",@"V2",@"k2",@"V3",@"k3", nil];
- //
- // NSArray *object=[NSArray arrayWithObjects:@"V1",@"V2",@"V3", nil];
- // NSArray *key=[NSArray arrayWithObjects:@"G1",@"G2",@"G3", nil];
- // dict=[NSDictionary dictionaryWithObjects:object forKeys:key];
- //
- // NSLog(@"%zi",dict.count);
- // NSLog(@"%@",dict);
- // 从字典中取值
- // id value=[dict objectForKey:@"k2"];
- // NSLog(@"%@",value);
- // 将字典中数据写入文件
- // NSString *path=@"/Users/fdf/Desktop/dict.xml";
- // [dict writeToFile:path atomically:YES];
- //将文件中的数据读取到字典之中
- NSString *path=@"/Users/fdf/Desktop/dict.xml";
- id dict1=[NSDictionary dictionaryWithContentsOfFile:path];
- NSLog(@"%@",dict1);
- }
- #pragma mark 字典的查询
- void dictionaryFor(){
- NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:@"V1",@"k1",@"V2",@"k2",@"V3",@"k3", nil];
- // NSArray *array=[dict allKeys];
- // NSArray *value=[dict allValues];
- // 传入多个key去查询value
- NSArray *value1= [dict objectsForKeys:[NSArray arrayWithObjects:@"k1",@"k2",@"k4", nil] notFoundMarker:@"not"];
- NSLog(@"%@ ",value1);
- }
- #pragma mark 字典的遍历
- void dictionaryIterator(){
- NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:@"V1",@"k1",@"V2",@"k2",@"V3",@"k3", nil];
- //快速遍历
- // for (id key in dict) {
- // id value=[ dict objectForKey:key];
- // NSLog(@"%@=%@",key,value);
- // }
- // while遍历
- // NSEnumerator *enums=[dict keyEnumerator];
- // id key=nil;
- // while (key=[enums nextObject]) {
- // NSLog(@"%@==%@",key,[dict objectForKey:key]);
- // }
- //block遍历
- [dict enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
- NSLog(@"%@==%@",key,obj);
- if ([key isEqualTo:@"k2"]) {//停止循环
- *stop=YES;
- }
- }];
- }
- #pragma mark 内存管理
- void dictionaryMemory(){
- NSMutableDictionary *dict=[NSMutableDictionary dictionary];
- Student *stu1=[Student studentWithName:@"stu1"];
- [dict setObject:stu1 forKey:@"k"];
- [dict removeObjectForKey:@"k"];
- NSLog(@"%zi",[ stu1 retainCount]);
- // Student *stu2=[Student studentWithName:@"stu2"];
- // Student *stu3=[Student studentWithName:@"stu3"];
- // NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:stu1,@"k1",stu2,@"k2",stu3,@"k3", nil];
- // NSMutableDictionary *dict=[NSMutableDictionary dictionaryWithObjectsAndKeys:stu1,@"k1",stu2,@"k2",stu3,@"k3", nil];
- // NSLog(@"%zi",[ stu1 retainCount]);
- // [dict removeAllObjects];
- // [dict removeObjectForKey:@"k2"];
- // [dict release];
- // [dict removeAllObjects];
- // NSLog(@"%zi",[ stu1 retainCount]);
- }
- int main(int argc, const char * argv[])
- {
- @autoreleasepool {
- dictionaryMemory();
- }
- return 0;
- }
- #import <Foundation/Foundation.h>
- @interface Student : NSObject
- @property (nonatomic,retain) NSString *name;
- +(id)studentWithName:(NSString *)name;
- @end
- #import "Student.h"
- @implementation Student
- #pragma mark 构造方法
- +(id)studentWithName:(NSString *)name{
- Student *stu= [[[Student alloc] init] autorelease];
- stu.name=name;
- return stu ;
- }
- #pragma mark 自动释放内存
- -(void)dealloc{
- NSLog(@"%@ 被销毁了",_name);
- [_name release];
- [super dealloc];
- }
- @end