//初始化
//1.1可以用新方法失现初始化
NSDictionary* dic = @{@"a":@"1",@"c":@"2",@"b":@"1",@"e":@"1",@"z":@"1"};
// NSDictionary* dic = @{@"0":@"1",@"3":@"2",@"2":@"1",@"4":@"1",@"1":@"1"};
NSDictionary *dic2 = @{@"key1":@"value1",@"key2":@"value2"};
NSLog(@"%@ %@",dic,dic2);
//1.2字典中有字典
NSDictionary *dic3 =@{@"key01":@"01",@"key0":@{@"key1": @"1",@"key2":@"2"}};
NSDictionary * dic4= dic3[@"key01"];
NSLog(@"字典中的另一字典%@",dic4);
//1.3+ (instancetype)dictionaryWithObjects:(NSArray *)objects forKeys:(NSArray> *)keys;
//另一种初始化字典方法,把key 和 value分别对应顺序的生成两个数组
NSArray *keyAry = @[@"key1", @"key2", @"key3"];
NSArray *objAry = [NSArray arrayWithObjects:@"value1", @"value2", @"value3",nil];
NSArray *otherAry = [NSArray arrayWithObjects:@"key1", @"key2",nil];
NSDictionary *dict = [NSDictionary dictionaryWithObjects:objAry forKeys:keyAry];
/*printf:
{
key1 = value1;
key2 = value2;
key3 = value3;
}
*/
//- (NSArray *)objectsForKeys:(NSArray *)keys notFoundMarker:(ObjectType)marker;
//根据otherAry中的key 查找 字典中的对应value值返回,如果不存在对应key则返回后面默认值
NSArray *objectAry = [dic objectsForKeys:otherAry notFoundMarker:@"11"];//notFoundMarker:[NSNull null]
//@property (readonly, copy) NSString *description;
//当前编码
NSString *descripStr = [dic description];
//@property (readonly, copy) NSString *descriptionInStringsFileFormat;
//编码转String
NSString *fileFormatStr = [dic descriptionInStringsFileFormat];
//- (NSString *)descriptionWithLocale:(nullable id)locale;
//声明分类重写该方法,本地化字符串打印
NSString *localeStr = [dic descriptionWithLocale:[NSLocale currentLocale]];
//- (NSString *)descriptionWithLocale:(nullable id)locale indent:(NSUInteger)level;
//声明分类重写该方法,本地化字符串打印+打印缩进
NSString *localeStrLev = [dic descriptionWithLocale:[NSLocale currentLocale] indent:2];
//- (BOOL)isEqualToDictionary:(NSDictionary *)otherDictionary;
//判断两个字典 key value是否相等
BOOL b = [dic isEqualToDictionary:dic2];
//- (NSEnumerator *)objectEnumerator;
//获取value集合
NSEnumerator *enumerator = [dic objectEnumerator];
id obj;
while( obj=[enumerator nextObject])
{
//遍历完后,元素已经清空,key相应销毁
NSLog(@"%@",obj);
}
NSArray *allAry = enumerator.allObjects;
//- (NSArray *)allKeysForObject:(ObjectType)anObject;
//获取value均为 @"指定值" 的 key
NSArray *allKeyFor = [dic allKeysForObject:@"1"];
//- (NSArray *)keysSortedByValueUsingSelector:(SEL)comparator;
//根据 value排序 ,返回排序后的 key数组
NSDictionary * numsDic = @{@3:@"3",
@1:@"1",
@2:@"2"};
NSArray * selKeys = [numsDic keysSortedByValueUsingSelector:@selector(compare:)];
NSLog(@"%@",selKeys.description);
//- (NSArray *)keysSortedByValueUsingComparator:(NSComparator)cmptr
//根据 value排序 ,返回排序后的 key数组
NSDictionary * strDic = @{@"2":@"dda",
@"3":@"ff",
@"1":@"a"};
NSArray * CompaKyes = [strDic keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSString * str1 = obj1;
NSString * str2 = obj2;
return [str1 compare:str2];
}];
NSLog(@"%@",CompaKyes.description);
//- (NSArray *)keysSortedByValueWithOptions:(NSSortOptions)opts usingComparator:(NSComparator)cmptr
//并发排序,和上面没区别,目前请都是对于内容是数字和字符排序
NSArray * optionsKeys = [numsDic keysSortedByValueWithOptions:NSSortConcurrent usingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
NSString * str1 = obj1;
NSString * str2 = obj2;
return [str1 compare:str2];
}];
//- (NSSet *)keysOfEntriesPassingTest:(BOOL (^)(KeyType key, ObjectType obj, BOOL *stop))predicate NS_AVAILABLE(10_6, 4_0);
NSSet *muSet = [numsDic keysOfEntriesPassingTest:^BOOL(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
return YES;
}];
//- (NSSet *)keysOfEntriesPassingTest:(BOOL (^)(KeyType key, ObjectType obj, BOOL *stop))predicate
//反向遍历 根据过滤条件,满足条件 return YES 告知方法把当前value传给 NSSet
NSDictionary * numsDic2 = @{@2:@"zzzzz",
@1:@"sssss",
@3:@"asdfsdf"};
NSSet * filteredKyes = [numsDic2 keysOfEntriesPassingTest:^BOOL(id key, id obj, BOOL *stop) {
//stop可以主动赋值为YES,让循环终止
if ([(NSString*)obj length]>1)
{
return YES;
}
return NO;
}];
NSLog(@"%@",filteredKyes.description);
//- (NSSet *)keysOfEntriesWithOptions:(NSEnumerationOptions)opts passingTest:(BOOL (^)(KeyType key, ObjectType obj, BOOL *stop))predicate
//反向遍历 根据过滤条件,满足条件 return YES 告知方法把当前value传给 NSSet
//注:这里NSEnumeration 枚举值不起作用
NSSet * optionsKyes = [numsDic2 keysOfEntriesWithOptions:NSEnumerationConcurrent passingTest:^BOOL(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
//stop可以主动赋值为YES,让循环终止
if ([(NSString*)obj length]>1)
{
return YES;
}
return NO;
}];
NSLog(@"%@",optionsKyes.description);
//
NSMutableDictionary *firDic = [NSMutableDictionary dictionaryWithObject:@"oneone" forKey:@"11"];
NSDictionary *getDic = firDic;// firDic值影响 getDic
NSDictionary *copyDic = [firDic copy]; // 值互不影响 copy=retain firDic引用计数不增加
NSDictionary *retainDic = [NSDictionary dictionaryWithDictionary:firDic]; //不影响
[getDic setValue:@"a" forKey:@"d"]; //不可变字典调用父类 NSObject setValue 方法,实现改变值
[getDic setValue:@"sdfsfa" forKey:@"d"];
[firDic setObject:@"two" forKey:@"22"];
[firDic setObject:@"three" forKey:@"333"];
NSLog(@"getDic===%@\n copyDic==%@\n retainDic==%@\n",getDic,copyDic,retainDic);
/*
2015-12-11 18:16:42.212 NSDictionary[2192:71986] getDic==={
11 = oneone;
22 = two;
333 = three;
}
copyDic=={
11 = oneone;
}
retainDic=={
11 = oneone;
}
以下是各自指针地址
(lldb) po &firDic
0x00007fff5be89900
(lldb) po &getDic
0x00007fff5be898f8
(lldb) po ©Dic
0x00007fff5be898f0
(lldb) po &retainDic
0x00007fff5be898e8
*/
Jason 字典 互换
//Dictionary 转 Json
NSDictionary *d = @{@"1":@"2",@"3":@"4",@"new":@[@{@"33":@"sdf"}]};
NSData *da = [NSJSONSerialization dataWithJSONObject:d options:NSJSONWritingPrettyPrinted error:nil];
NSString *s = [[NSString alloc] initWithData:da encoding:NSUTF8StringEncoding];
//Json 转 Dictionary
NSData *ddd = [s dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *d2 = [NSJSONSerialization JSONObjectWithData:ddd options:NSJSONReadingMutableContainers error:nil];