1、NSDictionary、NSMutableDictionary

可以理解为java里面的map,一个key对应一个value,key不可以重复

NSDictionary不可变,NSMutableDictionary可变

NSMutableDictionary比NSDictionary多了一些增加,删除,修改的函数

比如setObject:forKey:  removeObjectForKey:



2、测试Demo

NSDictionary+print.h

#import <Foundation/Foundation.h>



#ifndef NSDictionary_print_h
#define NSDictionary_print_h
@interface NSDictionary (print)
-(void)print;
@end


#endif /* NSDictionary_print_h */


NSDictionary+print.m

#import <Foundation/Foundation.h>
#import "NSDictionary+print.h"

@implementation NSDictionary(print)
-(void)print
{
NSMutableString *result = [NSMutableString stringWithFormat:@"{"];
for (id key in self)
{
[result appendString:[key description]];
[result appendString:@"="];
[result appendString:[self[key] description]];
[result appendString:@", "];
}
[result appendString:@"}"];
NSLog(@"%@", result);
}
@end


main.m

#import "NSDictionary+print.h"

int main(int argc, char * argv[]) {
@autoreleasepool {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"chenyu1", @"1", @"chenyu2", @"2", @"chenyu3", @"3", @"chenyu4", @"4", @"chenyu5", @"5",nil];
[dict print];
NSLog(@"dict有%ld个key-value", [dict count]);
NSLog(@"key is 2 value is %@", [dict[@"2"] description]);
NSLog(@"key is 2 value is %@", [dict objectForKey:@"2"]);
NSLog(@"dict 所有的key是:%@", [dict allKeys]);
NSEnumerator *en = [dict objectEnumerator];
id obj;
//遍历value
while (obj = [en nextObject])
{
NSLog(@"%@", obj);
}
[dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSLog(@"key的值为:%@", key);
NSLog(@"value的值为:%@", obj);
}];

NSMutableDictionary *muDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"chenyu1", @"1", @"chenyu2", @"2", @"chenyu3", @"3", @"chenyu4", @"4", @"chenyu5", @"5",nil];

[muDict print];

[muDict setObject:@"chen6" forKey:@"6"];
[muDict setObject:@"chen6" forKey:@"3"];
[muDict print];
[muDict removeObjectForKey:@"1"];
[muDict print];
}
}





3、运行结果

2018-07-19 23:40:03.444261+0800 cyTest[67110:13778920] {3=chenyu3, 1=chenyu1, 4=chenyu4, 2=chenyu2, 5=chenyu5, }
2018-07-19 23:40:03.446141+0800 cyTest[67110:13778920] dict有5个key-value
2018-07-19 23:40:03.446320+0800 cyTest[67110:13778920] key is 2 value is chenyu2
2018-07-19 23:40:03.446681+0800 cyTest[67110:13778920] key is 2 value is chenyu2
2018-07-19 23:40:03.446972+0800 cyTest[67110:13778920] dict 所有的key是:(
3,
1,
4,
2,
5
)
2018-07-19 23:40:03.447145+0800 cyTest[67110:13778920] chenyu3
2018-07-19 23:40:03.447340+0800 cyTest[67110:13778920] chenyu1
2018-07-19 23:40:03.447447+0800 cyTest[67110:13778920] chenyu4
2018-07-19 23:40:03.447578+0800 cyTest[67110:13778920] chenyu2
2018-07-19 23:40:03.447690+0800 cyTest[67110:13778920] chenyu5
2018-07-19 23:40:03.448208+0800 cyTest[67110:13778920] key的值为:3
2018-07-19 23:40:03.448322+0800 cyTest[67110:13778920] value的值为:chenyu3
2018-07-19 23:40:03.448422+0800 cyTest[67110:13778920] key的值为:1
2018-07-19 23:40:03.448515+0800 cyTest[67110:13778920] value的值为:chenyu1
2018-07-19 23:40:03.448613+0800 cyTest[67110:13778920] key的值为:4
2018-07-19 23:40:03.466511+0800 cyTest[67110:13778920] value的值为:chenyu4
2018-07-19 23:40:03.466823+0800 cyTest[67110:13778920] key的值为:2
2018-07-19 23:40:03.466983+0800 cyTest[67110:13778920] value的值为:chenyu2
2018-07-19 23:40:03.467161+0800 cyTest[67110:13778920] key的值为:5
2018-07-19 23:40:03.467334+0800 cyTest[67110:13778920] value的值为:chenyu5
2018-07-19 23:40:03.467602+0800 cyTest[67110:13778920] {3=chenyu3, 1=chenyu1, 4=chenyu4, 2=chenyu2, 5=chenyu5, }
2018-07-19 23:40:03.467838+0800 cyTest[67110:13778920] {3=chen6, 1=chenyu1, 6=chen6, 4=chenyu4, 2=chenyu2, 5=chenyu5, }
2018-07-19 23:40:03.468030+0800 cyTest[67110:13778920] {3=chen6, 6=chen6, 4=chenyu4, 2=chenyu2, 5=chenyu5, }