foundation-NSDictonary&NSMutableDictionary

来源:互联网 发布:大数据精准营销特点 编辑:程序博客网 时间:2024/05/23 23:18
////  main.m//  Foundation-NSDectionary////  Created by apple on 15/6/30.//  Copyright (c) 2015年 itcast. All rights reserved.//#import <Foundation/Foundation.h>#import "Student.h";#pragma mark 字典的创建void dictCreate(){    NSDictionary *dic = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"];    //最常用的初始化方式    dic = [NSDictionary dictionaryWithObjectsAndKeys:           @"v1",@"k1",           @"v2",@"k2",           @"v3",@"k3",           @"v4",@"k4",nil];    NSLog(@"%@",dic);        NSArray *objects = [NSArray arrayWithObjects:@"v7",@"v8",@"v9", nil];    NSArray *keys = [NSArray arrayWithObjects:@"k7",@"k8",@"k9", nil];    dic = [NSDictionary dictionaryWithObjects:objects forKeys:keys];    NSLog(@"%@",dic);    }#pragma mark 字典基本使用void dictUse(){    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:           @"v1",@"k1",           @"v2",@"k2",           @"v3",@"k3",           @"v4",@"k4",nil];    //有多少键值对    NSUInteger count = [dic count];    NSLog(@"count = %ld",count);    //取值,不能改    NSString *obj = [dic objectForKey:@"k2"];    NSLog(@"%@",obj);    //将字典写入文件    NSString *path = @"/Users/apple/Desktop/dict.xml";    //[dic writeToFile:path atomically:YES];    //从文件中读取字典    NSDictionary *dict2 = [NSDictionary dictionaryWithContentsOfFile:path];    NSLog(@"%@",dict2);}#pragma mark 字典基本使用void dictUse2(){    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:                         @"v1",@"k1",                         @"v2",@"k2",                         @"v3",@"k3",                         @"v6",@"k4",                         @"v7",@"k5",                         @"v19",@"k6",                         @"v4",@"k7",nil];    //返回所有的key,无序的!    NSArray *arr = [dic allKeys];    //NSLog(@"%@",arr);    //NSArray *arr2 = [dic allValues];    //NSLog(@"%@",arr2);    //key找不到是用marker代替    arr = [dic objectsForKeys:[NSArray arrayWithObjects:@"k1",@"k19", nil] notFoundMarker:@"not-found"];    NSLog(@"%@",arr);}#pragma mark 字典的遍历void dictFor(){    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:                         @"v1",@"k1",                         @"v2",@"k2",                         @"v3",@"k3",                         @"v4",@"k4",nil];    for(id key in dic){        id value = [dic objectForKey:key];        NSLog(@"%@ = %@",key,value);    }}#pragma mark 字典的遍历2void dictFor2(){    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:                         @"v1",@"k1",                         @"v2",@"k2",                         @"v3",@"k3",                         @"v4",@"k4",nil];    NSEnumerator *enumer = [dic keyEnumerator];    id key = nil;    while (key = [enumer nextObject]) {        id value = [dic objectForKey:key];        NSLog(@"%@=%@",key,value);    }}#pragma mark 字典的遍历3void dictFor3(){    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:                         @"v1",@"k1",                         @"v2",@"k2",                         @"v3",@"k3",                         @"v4",@"k4",nil];    [dic enumerateKeysAndObjectsUsingBlock:     ^(id key, id obj, BOOL *stop) {        NSLog(@"%@=%@",key,obj);    }];}#pragma mark 内存管理void memory(){    Student *stu1 = [Student studentWithName:@"peter1"];    Student *stu2 = [Student studentWithName:@"peter2"];    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:                         stu1,@"k1",                         stu2,@"k2",nil];}int main(int argc, const char * argv[]) {    @autoreleasepool {        dictFor3();    }    return 0;}


////  Student.m//  Foundation-NSDectionary////  Created by apple on 15/6/30.//  Copyright (c) 2015年 itcast. All rights reserved.//#import "Student.h"@implementation Student+(id)studentWithName:(NSString *)name{    Student *stu = [[Student alloc]init];    stu.name = name;    return stu;}- (void)dealloc{    NSLog(@"%@对象被销毁了",_name);}@end

////  Student.h//  Foundation-NSDectionary////  Created by apple on 15/6/30.//  Copyright (c) 2015年 itcast. All rights reserved.//#import <Foundation/Foundation.h>@interface Student : NSObject@property (nonatomic,retain)NSString *name;+(id)studentWithName:(NSString *)name;@end


NSMutableDictionary:

#import <Foundation/Foundation.h>#import "Student.h"int main(int argc, const char * argv[]) {    @autoreleasepool {        NSMutableDictionary *dic = [NSMutableDictionary dictionary];        Student *stu1 = [Student studentWithName:@"peter1"];        Student *stu2 = [Student studentWithName:@"peter2"];        [dic setObject:stu1 forKey:@"k1"];        [dic setObject:stu2 forKey:@"k2"];                [dic removeAllObjects];    }    return 0;}




0 0
原创粉丝点击