閑話少說,先上Demo
Github地址:https://github.com/Jerryisme/DictionaryUsage
NSDictionary官方API文檔
NSMutableDictionary官方API文檔
常用方法
不可變字典
#pragma mark - 不可變字典
- (void)setUpDictionary {
// 字典 所以 ---> 文字內(nèi)容(key ---> value) 里面存儲的都是鍵值對
NSDictionary *dic0 = [NSDictionary dictionary];
NSLog(@"創(chuàng)建空字典--- %@\n", dic0);
NSDictionary *dic1 = [NSDictionary dictionaryWithObject:@"jerry" forKey:@"name"];
NSLog(@"創(chuàng)建字典dic1--- %@\n", dic1);
// 查找key對應的value
id obj1 = [dic1 objectForKey:@"name"];
NSLog(@"查找key對應的value obj1--- %@\n", obj1);
NSDictionary *dic2 = [NSDictionary dictionaryWithObjects:@[@"jerry", @"3"] forKeys:@[@"name", @"old"]];
NSLog(@"創(chuàng)建字典dic2--- %@\n", dic2);
id obj2 = [dic2 objectForKey:@"old"];
NSLog(@"查找key對應的value obj2--- %@\n", obj2);
// 很少用
NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"jerry", @"name", @"3", @"old", @"man", @"sex", nil];
NSLog(@"創(chuàng)建字典dic3--- %@\n", dic3);
id obj3 = [dic3 objectForKey:@"sex"];
NSLog(@"查找key對應的value obj3--- %@\n", obj3);
NSDictionary *dic4 = @{@"address":@"shanghai", @"birthday":@"2000-01-01"};
NSLog(@"創(chuàng)建字典dic4--- %@\n", dic4);
id obj4 = dic4[@"address"];
NSLog(@"查找key對應的value obj4--- %@\n", obj4);
// 獲取字典鍵值對個數(shù)
NSLog(@"獲取字典鍵值對個數(shù)--- %ld\n", dic4.count);
// 獲取字典中所有key
NSLog(@"獲取字典中所有key--- %@\n", [dic4 allKeys]);
NSEnumerator *enumKeys = [dic4 keyEnumerator];
for (NSObject *obj in enumKeys){
NSLog(@"enumKey: %@", obj);
}
for (NSObject *obj in dic4){
NSLog(@"key in dict: %@", obj);
}
// 獲取字典中所有value
NSLog(@"獲取字典中所有value--- %@\n", [dic4 allValues]);
NSEnumerator *enumValues = [dic4 objectEnumerator];
for (NSObject *obj in enumValues){
NSLog(@"value in dict: %@", obj);
}
// 字典寫入沙盒中荠呐,沙盒中讀取字典
[self writeAndContents];
}
可變字典
#pragma mark - 可變字典
- (void)setUpNSMutableDictionary {
NSMutableDictionary *mutableDic = [@{@"address":@"shanghai", @"birthday":@"2000-01-01", @"name":@"haha"} mutableCopy];
NSLog(@"創(chuàng)建字典mutableDic--- %@\n", mutableDic);
// 添加鍵值對
NSMutableDictionary *mutableDic0 = [NSMutableDictionary dictionary];
[mutableDic0 setObject:@"jerry" forKey:@"name"];
[mutableDic0 setObject:@"3" forKey:@"old"];
[mutableDic0 setObject:@"tom" forKey:@"name"];
id obj = mutableDic0[@"name"];
NSLog(@"查找key對應的value obj--- %@\n", obj);
// 將一個字典鍵值添加到另外一個字典中,如果B里面key如果A里面存在相同的key的話也會替換value
[mutableDic addEntriesFromDictionary:mutableDic0];
NSLog(@"將一個字典鍵值添加到另外一個字典中呵恢,如果B里面key如果A里面存在相同的key的話也會替換valuemutableDic--- %@\n", mutableDic);
[mutableDic setValuesForKeysWithDictionary:@{@"love":@"eat", @"name":@"TT"}];
NSLog(@"把B里面鍵值對添加字典A里面--- %@\n", mutableDic);
// 替換整個字典
[mutableDic setDictionary:mutableDic0];
NSLog(@"替換整個字典--- %@\n", mutableDic);
// 可以指定“鍵”的方式刪除字典中對應的“鍵值對”渗钉,如果該Key不存在,則不做任何操作
[mutableDic0 removeObjectForKey:@"old"];
// 清空整個字典的數(shù)據(jù)
[mutableDic0 removeAllObjects];
}
字典寫入沙盒中鳄橘,沙盒中讀取字典
#pragma mark - 字典寫入沙盒中芒炼,沙盒中讀取字典
- (void)writeAndContents {
// 獲取Documents目錄
NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 字典寫入文件
// 創(chuàng)建一個存儲字典的文件路徑
NSString *fileDicPath = [docPath stringByAppendingPathComponent:@"bug.txt"];
NSDictionary *dic = @{@"name":@"jerry", @"old":@"3"};
// 字典寫入時執(zhí)行的方法
[dic writeToFile:fileDicPath atomically:YES];
NSLog(@"字典寫入時執(zhí)行的方法--- %@\n", fileDicPath);
// 從文件中讀取數(shù)據(jù)字典的方法
NSDictionary *resultDic = [NSDictionary dictionaryWithContentsOfFile:fileDicPath];
NSLog(@"從文件中讀取數(shù)據(jù)字典的方法--- %@\n", resultDic);
}
字典轉(zhuǎn)化字符串
#pragma mark - 字典轉(zhuǎn)化字符串
+ (NSString*)dictionaryToJson:(NSDictionary *)dic
{
NSError *parseError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
}
字符串轉(zhuǎn)字典
#pragma mark - 字符串轉(zhuǎn)字典
+ (NSDictionary *)dictionaryWithJsonString:(NSString *)jsonString
{
if (jsonString == nil) {
return nil;
}
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSError *err;
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
if(err) {
NSLog(@"json解析失斁ㄅ取:%@",err);
return nil;
}
return dic;
}