iOS官方文档 Foundation篇---NSDictionary(NSMutableDictionary)

天其高,无日月星辰,不成神话;地其阔,无万物百态,不成人间。

——解锋镝

NSDictionary

与唯一键关联的静态对象集合(不可变字典,键值对);

#创建
NSDictionary *dict = @{
                       @"key1" : @"someObject",
                       @"key2" : @"Hello-World!",
                       @"key3" : @"42",
                       @"key4" : @"someValue",
                       @"key5" : @"someObject"
                       };
NSDictionary *dict1 = [NSDictionary dictionary];
//值数组
NSArray *objects = @[@"someObject", @"Hello, World!", @42, @"someValue" ];
//键数组
NSArray *keys = @[ @"anObject", @"helloString", @"magicNumber", @"aValue" ];
//根据值数组与键数组创建字典
NSDictionary *dict2 = [NSDictionary dictionaryWithObjects:objects forKeys:keys];


id objectss[] = { @"someObject", @"Hello, World!", @42, @"someValue" };
id keyss[] = { @"anObject", @"helloString", @"magicNumber", @"aValue" };
//根据值数组,键数组和键值对个数创建字典
NSDictionary *dict3 = [NSDictionary dictionaryWithObjects:objectss forKeys:keyss count:2];
//键值创建字典
NSDictionary *dict4 = [NSDictionary dictionaryWithObject:@"hellow" forKey:@"key"];
NSDictionary *dict5 = [NSDictionary dictionaryWithObjectsAndKeys:@"hellow",@"firstKey",@"world",@"secondKey", nil];
NSDictionary *dict6 = [NSDictionary dictionaryWithDictionary:dict3];
#取值
//字典包含的键值对个数
NSUInteger countss = dict.count;//5

//根据键取值
NSString *object = [dict objectForKey:@"key1"];//someObject

//返回指定对象所对应的键值数组
NSArray *keys = [dict allKeysForObject:@"someObject"];//key1,key5

//返回包含字典中所有的值的数组
NSArray *allValues = dict.allValues;//42,someObject,someValue,Hello-World!,someObject

//返回包含字典中所有的键的数组
NSArray *allKeys = dict.allKeys;//key3,key1,key4,key2,key5

//返回字典内容表示的字符串对象
NSString *str = dict.description;//{key1 = someObject;key2 = "Hello-World!";key3 = 42;key4 = someValue;key5 = someObject;}

//返回字典内容的字符串对象
NSString *str = dict.descriptionInStringsFileFormat;//"key3" = 42;"key1" = "someObject";"key4" = "someValue";"key2" = "Hello-World!";"key5" = "someObject";

//返回表示字典内容的字符串对象,格式为属性列表
NSString *str = [dict descriptionWithLocale:nil];//"key3" = 42;"key1" = "someObject";"key4" = "someValue";"key2" = "Hello-World!";"key5" = "someObject";

//返回BOOL值,判断字典是否相等
BOOL isEqual = [dict isEqualToDictionary:dict1];//NO

NSArray *keysArr = @[@"key1",@"key2",@"nil",@"Nil",@"null"];
//从字典中返回指定键所对应的值,如果无对应值返回默认值
NSArray *typeArr = [dict objectsForKeys:keysArr notFoundMarker:@"1"];//someObject,Hello-World!,1,1,1

//返回与给定键关联的值
NSString *objectt = [dict objectForKeyedSubscript:@"key1"];//someObject
#遍历
//遍历字典中的键
NSEnumerator *enumerator = [dict keyEnumerator];
id key;
while (key = [enumerator nextObject]) {
  NSLog(@"字典正序键开始打印:%@\n",key);
}

//遍历字典中的值
NSEnumerator *enumerator1 = [dict objectEnumerator];
id obj;
while (obj = [enumerator1 nextObject]) {
  NSLog(@"字典正序值开始打印:%@\n",obj);
}

//遍历键值对
for (NSString *key in dict) {
  NSString *value = dict[key];
  NSLog(@"%@--%@",key,value);
}

//遍历键值对
[dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
  NSLog(@"%@--%@",key,obj);
}];

/*
 NSEnumerationOptions
 NSEnumerationConcurrent   //无序遍历
 NSEnumerationReverse      //倒序遍历
 */
//遍历键值对
[dict enumerateKeysAndObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull key, id  _Nonnull
obj, BOOL * _Nonnull stop) {
  NSLog(@"%@--%@",key,obj);
}];
#字典排序

//通过value给key排序
NSArray *sortArr = [dict keysSortedByValueUsingSelector:@selector(compare:)];//key3,key2,key1,key5,key4

//根据值排序返回键数组
NSArray *sortArray = [dict keysSortedByValueUsingComparator:^NSComparisonResult(id _Nonnull obj1,id  _Nonnull obj2) {
  return NSOrderedDescending;
}];//key5,key2,key4,key1,key3

/*
 SSortOptions:
 NSSortConcurrent    高效的但不稳定的排序算法
 NSSortStable        稳定的排序算法
 */
//根据值排序返回键数组
NSArray *sortArry1 = [dict keysSortedByValueWithOptions:NSSortStable usingComparator:^NSComparisonResult(id  _Nonnull obj1,id _Nonnull obj2) {
  return NSOrderedAscending;
}];//key3,key1,key4,key2,key5

//返回对应值满足条件的键集。
NSSet *set = [dict keysOfEntriesPassingTest:^BOOL(id  _Nonnull key, id  _Nonnull obj, BOOL *
_Nonnull stop) {
  NSLog(@"%@--%@",key,obj);
  return [obj length] > 9;
}];// key1,key2,key5


//返回对应值满足条件的键集。
/*
 NSEnumerationOptions
 NSEnumerationConcurrent   //无序遍历
 NSEnumerationReverse      //倒序遍历
 */
NSSet *set1 = [dict keysOfEntriesWithOptions:NSEnumerationReverse passingTest:^BOOL(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
  NSLog(@"%@--%@",key,obj);
  return [obj length] < 9;
}];// key3

NSMutableDictionary

与唯一键关联的动态对象集合(可变字典,继承自NSDictionary);

#创建

//正常创建字典
NSDictionary *dict = @{
                       @"key1" : @"someObject",
                       @"key2" : @"Hello-World!",
                       @"key3" : @"42",
                       @"key4" : @"someValue",
                       @"key5" : @"someObject"
                       };
NSDictionary *dict1 = [NSDictionary dictionaryWithObjectsAndKeys:@"object1",@"key6",@"object2",@"key7",@"object3",@"key8", nil];
NSMutableDictionary *mulDict = [NSMutableDictionary dictionaryWithDictionary:dict];
NSMutableDictionary *mulDict1 = [NSMutableDictionary dictionary];
#增删改查

//将给定的键值对添加到字典中(键不同则增加键值对,键相同更改键对应的值)
 [mulDict setObject:@"obj" forKey:@"key"];
 [mulDict setObject:@"obje" forKeyedSubscript:@"keyy"];
//将字典中的键值对设置为给定字典中的键值对(完全替换)
 [mulDict setDictionary:dict];

//根据键移除键值对
 [mulDict removeObjectForKey:@"key1"];

//根据键数组移除对应的值
 [mulDict removeObjectsForKeys:@[@"key2",@"key3"]];

//移除字典中所有的键值对
 [mulDict removeAllObjects];
 
//在原字典中添加字典中的键值对
 [mulDict addEntriesFromDictionary:dict1];
欢迎留言指正,会持续更新。。。

你可能感兴趣的:(iOS官方文档 Foundation篇---NSDictionary(NSMutableDictionary))