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
- foundation-NSDictonary&NSMutableDictionary
- Foundation--NSMutableDictionary
- Foundation NSMutableDictionary
- Foundation-NSMutableDictionary
- Foundation--NSMutableDictionary
- 【Foundation Frame】NSDictionary/NSMutableDictionary
- Foundation中的NSDictionary与NSMutableDictionary
- Foundation 之 NSDictionary和NSMutableDictionary
- Foundation框架中的NSDictionary 、NSMutableDictionary
- Objective-C之Foundation框架的NSMutableDictionary
- Foundation框架基础 NSMutableDictionary-可变字典
- Foundation框架 - NSDictionary类、NSMutableDictionary类
- 22 Foundation框架之NSDictionary和NSMutableDictionary
- Foundation框架学习之NSDictionary和NSMutableDictionary
- 黑马程序员——Foundation学习—-----NSDictionary和NSMutableDictionary
- 【Foundation学习笔记】常用类--NSDictionary和NSMutableDictionary
- 黑马程序员——Foundation学习之NSDictionary\NSMutableDictionary
- 黑马程序员—Foundation框架之NSDictionary与NSMutableDictionary
- LeetCode 之 Populating Next Right Pointers in Each Node I II — C++ 实现
- mysql 从的一些参数设置
- 10款超炫的开源jquery的源码预览效果
- error LNK2019: 无法解析的外部符号 "void __cdecl cv::calcHist
- SPI协议简介
- foundation-NSDictonary&NSMutableDictionary
- Clone Graph DFS&BFS 图的复制
- 出走一厦门篇
- 智能听音公司Soundhawk获得550万美元融资,通过降噪算法让用户听到真正想听的声音
- 35.app后端搜索入门
- javax.swing.Timer 使用
- 黑马程序员——Java基础---线程的另一个总结(7)--condition,await
- 出走二杭州篇
- Service的生命周期(一)