IOS学习笔记二十一(NSDictionary、NSMutableDictionary)

举报
chenyu 发表于 2021/07/27 00:11:37 2021/07/27
【摘要】 1、NSDictionary、NSMutableDictionary 可以理解为java里面的map,一个key对应一个value,key不可以重复 NSDictionary不可变,NSMutableDictionary可变 NSMutableDictionary比NSDictionary多了一些增加,删除,修改的函数 比如setObject:forKey:&nbsp...

1、NSDictionary、NSMutableDictionary

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

NSDictionary不可变,NSMutableDictionary可变

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

比如setObject:forKey:  removeObjectForKey:

 

 

 

 

 

 

 

 

2、测试Demo

NSDictionary+print.h


  
  1. #import <Foundation/Foundation.h>
  2. #ifndef NSDictionary_print_h
  3. #define NSDictionary_print_h
  4. @interface NSDictionary (print)
  5. -(void)print;
  6. @end
  7. #endif /* NSDictionary_print_h */

 

NSDictionary+print.m


  
  1. #import <Foundation/Foundation.h>
  2. #import "NSDictionary+print.h"
  3. @implementation NSDictionary(print)
  4. -(void)print
  5. {
  6. NSMutableString *result = [NSMutableString stringWithFormat:@"{"];
  7. for (id key in self)
  8. {
  9. [result appendString:[key description]];
  10. [result appendString:@"="];
  11. [result appendString:[self[key] description]];
  12. [result appendString:@", "];
  13. }
  14. [result appendString:@"}"];
  15. NSLog(@"%@", result);
  16. }
  17. @end

 

main.m


  
  1. #import "NSDictionary+print.h"
  2. int main(int argc, char * argv[]) {
  3. @autoreleasepool {
  4. NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"chenyu1", @"1", @"chenyu2", @"2", @"chenyu3", @"3", @"chenyu4", @"4", @"chenyu5", @"5",nil];
  5. [dict print];
  6. NSLog(@"dict有%ld个key-value", [dict count]);
  7. NSLog(@"key is 2 value is %@", [dict[@"2"] description]);
  8. NSLog(@"key is 2 value is %@", [dict objectForKey:@"2"]);
  9. NSLog(@"dict 所有的key是:%@", [dict allKeys]);
  10. NSEnumerator *en = [dict objectEnumerator];
  11. id obj;
  12. //遍历value
  13. while (obj = [en nextObject])
  14. {
  15. NSLog(@"%@", obj);
  16. }
  17. [dict enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  18. NSLog(@"key的值为:%@", key);
  19. NSLog(@"value的值为:%@", obj);
  20. }];
  21. NSMutableDictionary *muDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"chenyu1", @"1", @"chenyu2", @"2", @"chenyu3", @"3", @"chenyu4", @"4", @"chenyu5", @"5",nil];
  22. [muDict print];
  23. [muDict setObject:@"chen6" forKey:@"6"];
  24. [muDict setObject:@"chen6" forKey:@"3"];
  25. [muDict print];
  26. [muDict removeObjectForKey:@"1"];
  27. [muDict print];
  28. }
  29. }

 

 

 

 

3、运行结果


  
  1. 2018-07-19 23:40:03.444261+0800 cyTest[67110:13778920] {3=chenyu3, 1=chenyu1, 4=chenyu4, 2=chenyu2, 5=chenyu5, }
  2. 2018-07-19 23:40:03.446141+0800 cyTest[67110:13778920] dict有5个key-value
  3. 2018-07-19 23:40:03.446320+0800 cyTest[67110:13778920] key is 2 value is chenyu2
  4. 2018-07-19 23:40:03.446681+0800 cyTest[67110:13778920] key is 2 value is chenyu2
  5. 2018-07-19 23:40:03.446972+0800 cyTest[67110:13778920] dict 所有的key是:(
  6. 3,
  7. 1,
  8. 4,
  9. 2,
  10. 5
  11. )
  12. 2018-07-19 23:40:03.447145+0800 cyTest[67110:13778920] chenyu3
  13. 2018-07-19 23:40:03.447340+0800 cyTest[67110:13778920] chenyu1
  14. 2018-07-19 23:40:03.447447+0800 cyTest[67110:13778920] chenyu4
  15. 2018-07-19 23:40:03.447578+0800 cyTest[67110:13778920] chenyu2
  16. 2018-07-19 23:40:03.447690+0800 cyTest[67110:13778920] chenyu5
  17. 2018-07-19 23:40:03.448208+0800 cyTest[67110:13778920] key的值为:3
  18. 2018-07-19 23:40:03.448322+0800 cyTest[67110:13778920] value的值为:chenyu3
  19. 2018-07-19 23:40:03.448422+0800 cyTest[67110:13778920] key的值为:1
  20. 2018-07-19 23:40:03.448515+0800 cyTest[67110:13778920] value的值为:chenyu1
  21. 2018-07-19 23:40:03.448613+0800 cyTest[67110:13778920] key的值为:4
  22. 2018-07-19 23:40:03.466511+0800 cyTest[67110:13778920] value的值为:chenyu4
  23. 2018-07-19 23:40:03.466823+0800 cyTest[67110:13778920] key的值为:2
  24. 2018-07-19 23:40:03.466983+0800 cyTest[67110:13778920] value的值为:chenyu2
  25. 2018-07-19 23:40:03.467161+0800 cyTest[67110:13778920] key的值为:5
  26. 2018-07-19 23:40:03.467334+0800 cyTest[67110:13778920] value的值为:chenyu5
  27. 2018-07-19 23:40:03.467602+0800 cyTest[67110:13778920] {3=chenyu3, 1=chenyu1, 4=chenyu4, 2=chenyu2, 5=chenyu5, }
  28. 2018-07-19 23:40:03.467838+0800 cyTest[67110:13778920] {3=chen6, 1=chenyu1, 6=chen6, 4=chenyu4, 2=chenyu2, 5=chenyu5, }
  29. 2018-07-19 23:40:03.468030+0800 cyTest[67110:13778920] {3=chen6, 6=chen6, 4=chenyu4, 2=chenyu2, 5=chenyu5, }

 

文章来源: chenyu.blog.csdn.net,作者:chen.yu,版权归原作者所有,如需转载,请联系作者。

原文链接:chenyu.blog.csdn.net/article/details/81124636

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。