字典是以键值对的形式来存储数据 key value
1 NSDictionary 字典
1.1 创建字典,不可变的
NSDictionary * dic = [NSDictionary dictionaryWithObjectsAndKeys:@"xiaozhe",@"name", nil]; NSLog(@"dic %@",dic);
2016-08-14 14:44:17.460 07-字典类[2325:547877] dic {
name = xiaozhe;
}
1.2 快捷创建方式
NSDictionary * dic2 = @{ @"one":@"1",@"two":@"2"}; NSLog(@"dic2 %@",dic2);
2016-08-14 14:44:17.461 07-字典类[2325:547877] dic2 {
one = 1;
two = 2;
}
1.3 字典中可以存任意数据类型
字典的顺序不是自然顺序
NSArray * array = @[@"one",@"two"];
NSDictionary * dic3 = @{
@"one":@"1", @"num":[NSNumber numberWithInt:10], @"aaa":dic2, @"bbb":dic, @"ar1":array }; NSLog(@"dic3 %@",dic3);
016-08-14 14:44:17.461 07-字典类[2325:547877] dic3 { aaa = { one = 1; two = 2; }; ar1 = ( one, two ); bbb = { name = xiaozhe; }; num = 10; one = 1; }
1.4 获得字典的长度
NSLog(@"count %ld",dic3.count);
1.5 从字典中取值
NSString * str = [dic3 objectForKey:@"one"];
NSLog(@"str %@",str);
NSDictionary * dicTmp = [dic3 objectForKey:@"aaa"]; NSLog(@"dicTmp %@",dicTmp); NSArray * arrayTmp = [dic3 objectForKey:@"ar1"]; NSLog(@"arrayTmp %@",arrayTmp);
1.6 遍历
取出所有的key值
NSArray * allkeys = [dic3 allKeys];
NSLog(@"allkeys %@",allkeys);
for (int i = 0; i < allkeys.count; i++) { NSString * key = [allkeys objectAtIndex:i]; //如果你的字典中存储的多种不同的类型,那么最好用id类型去接受它 id obj = [dic3 objectForKey:key]; NSLog(@"obj %@",obj); }
枚举器
NSEnumerator * enumerator = [dic3 objectEnumerator];
id value;
while (value = [enumerator nextObject]) {
NSLog(@"value %@",value); }
2 NSMutableDictionary 可变字典
2.1 创建一个可变长度字典
NSMutableDictionary * muDic = [[NSMutableDictionary alloc] initWithCapacity:0];
2.2 向字典中存储数据
[muDic setObject:@"1" forKey:@"one"];
[muDic setObject:@"2" forKey:@"two"]; [muDic setObject:@"3" forKey:@"three"];
2.3 删除
[muDic removeObjectForKey:@"one"];
2.4 全部删除
[muDic removeAllObjects];
给一个 Student 类
@interface Student : NSObject
@property (nonatomic,assign) int age; @property (nonatomic,strong) NSString * name; - (id)initWithName:(NSString *)name andAge:(int)age; @end
@implementation Student
- (id)initWithName:(NSString *)name andAge:(int)age { if (self = [super init]) { _name = name; _age = age; } return self; } - (NSString *)description { return [NSString stringWithFormat:@"name %@ age %d",_name,_age]; } @end
Student * stu1 = [[Student alloc] initWithName:@"xiaoher" andAge:20];
Student * stu2 = [[Student alloc] initWithName:@"alex" andAge:50];
Student * stu3 = [[Student alloc] initWithName:@"xiaoli" andAge:10]; [muDic setObject:stu1 forKey:@"s1"]; [muDic setObject:stu2 forKey:@"s2"]; [muDic setObject:stu3 forKey:@"s3"]; //在向字典中存储数据的时候,一定要保证key值是唯一的 //[muDic setObject:stu3 forKey:@"s3"]; //[muDic setObject:stu3 forKey:@"s3"]; //[muDic setObject:stu3 forKey:@"s3"];
2.5 使用for循环遍历字典
NSArray * allkeys = [muDic allKeys];
for (int i = 0; i < allkeys.count; i++) { NSString * key = [allkeys objectAtIndex:i]; Student * stu = [muDic objectForKey:key]; NSLog(@"stu %@",stu); };
2.6 使用枚举器
NSEnumerator * enumerator = [muDic objectEnumerator];
Student * tmp;
while (tmp = [enumerator nextObject]) {
NSLog(@"tmp %@",tmp);
}