IOS學(xué)習(xí)筆記二十一(NSDictionary、NSMutableDictionary)
1、NSDictionary、NSMutableDictionary
可以理解為java里面的map,一個(gè)key對應(yīng)一個(gè)value,key不可以重復(fù)
NSDictionary不可變,NSMutableDictionary可變
NSMutableDictionary比NSDictionary多了一些增加,刪除,修改的函數(shù)
比如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個(gè)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、運(yùn)行結(jié)果
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個(gè)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, }
作者:chen.yu
深信服三年半工作經(jīng)驗(yàn),目前就職游戲廠商,希望能和大家交流和學(xué)習(xí),
微信公眾號:編程入門到禿頭 或掃描下面二維碼
零基礎(chǔ)入門進(jìn)階人工智能(鏈接)