前面我们讲了NSDictionary, 现在我们来讲一下它的子类NSMutableDictionary, 这个NSMutableDictionary和前面的NSMutableString, NSMutableArray是一样的原理, 都是可变的, 下面让我们来看看它是怎么运作的:
例子:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"jim" forKey:@"name"];
[dic setObject:@"18" forKey:@"age"];
id obj = dic[@"name"];
id obj2 = dic[@"age"];
NSLog(@"obj = %@, obj2 = %@", obj, obj2);
return 0;
}
打印出来的结果:
2015-02-05 23:18:15.963 9.NSMutableDictionary[4210:303] obj = jim, obj2 = 18
在前面我说过, 一个键就只能对应一个值, 那如果有一个键对应多个值会发生什么事呢? 下面来看看:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"jim" forKey:@"name"];
[dic setObject:@"18" forKey:@"age"];
[dic setObject:@"30" forKey:@"age"];
id obj = dic[@"name"];
id obj2 = dic[@"age"];
NSLog(@"obj = %@, obj2 = %@", obj, obj2);
return 0;
}
打印出来的结果:
2015-02-05 23:20:12.871 9.NSMutableDictionary[4220:303] obj = jim, obj2 = 30
从上面的结果, 我们可以看到, 键不变, 改变的是值, 其实这是没错的, 无论是在NSDictionary中还是NSMutableDictionary里, 只要是有相同的键值对, 在后面的值会覆盖掉前面的值.
当然, 有添加那就肯定有删除的方法, 比如:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSMutableDictionary *dic = [NSMutableDictionary dictionary];
[dic setObject:@"jim" forKey:@"name"];
[dic setObject:@"18" forKey:@"age"];
[dic removeObjectForKey:@"name"];
NSLog(@"%@", dic);
return 0;
}
打印出来的结果:
2015-02-05 23:23:36.041 9.NSMutableDictionary[4233:303] {
age = 30;
}
遍历方法:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSDictionary *dic = @{@"name" : @"rose", @"age" : @"20"};
NSArray *keys = [dic allKeys];
for(int i = 0; i < dic.count; i++)
{
NSString *key = keys[i];
NSString *objes = dic[key];
NSLog(@"%@ --- %@", key, objes);
}
return 0;
}
打印出来的结果:
2015-02-05 23:37:44.102 9.NSMutableDictionary[4272:303] name --- rose
2015-02-05 23:37:44.104 9.NSMutableDictionary[4272:303] age --- 20
里面的语法之前都有讲过, 这里就不多讲了, 想知道的话, 回去翻翻我之前的博文吧~~
block遍历:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSDictionary *dic = @{
@"name" : @"rose",
@"age" : @"20",
@"age1" : @"20",
@"age2" : @"20"};
[dic enumerateKeysAndObjectsUsingBlock:
^(id key, id obj, BOOL *stop) {
NSLog(@"%@ --- %@", key, obj);
}];
return 0;
}
打印出来的结果:
2015-02-06 12:57:48.647 9.NSMutableDictionary[4473:303] age --- 20
2015-02-06 12:57:48.648 9.NSMutableDictionary[4473:303] age1 --- 20
2015-02-06 12:57:48.649 9.NSMutableDictionary[4473:303] age2 --- 20
2015-02-06 12:57:48.649 9.NSMutableDictionary[4473:303] name --- rose
看完这个block遍历, 你应该就明白我为什么会说Dictionary是没有顺序了, 回想一下我们的NSArray遍历, 在block的遍历中, 我们看到一个Index的一个参数, 而在NSDictionary中, 压根就没有这个参数, 所以我一直强调, Dictionary是没有顺序的.
这里补充一个NSDictionary的知识点, 一个Dictionary允许有多个相同的值存在, 但不允许有多个相同的键存在, 下面我们来看看:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
NSDictionary *dic = @{
@"name" : @"rose",
@"age" : @"20",
@"age1" : @"20",
@"age2" : @"20"};
NSArray *keys = [dic allKeys];
for(int i = 0; i < dic.count; i++)
{
NSString *key = keys[i];
NSString *objes = dic[key];
NSLog(@"%@ --- %@", key, objes);
}
return 0;
}
打印出来的结果:
2015-02-06 12:17:34.997 9.NSMutableDictionary[4392:303] age --- 20
2015-02-06 12:17:34.999 9.NSMutableDictionary[4392:303] age1 --- 20
2015-02-06 12:17:35.000 9.NSMutableDictionary[4392:303] age2 --- 20
2015-02-06 12:17:35.000 9.NSMutableDictionary[4392:303] name --- rose
这里同样说明了另一个问题, 就是NSDictionary是没有顺序的, 看看例子看看打印出来的结果就知道了.
总结:
集合
1.NSArray\NSMutableArray
* 有序
* 快速创建(不可变):@[obj1, obj2, obj3]
* 快速访问元素:数组名[i]
2.NSSet\NSMutableSet
* 无序
3.NSDictionary\NSMutableDictionary
* 无序
* 快速创建(不可变):@{key1 : value1, key2 : value2}
* 快速访问元素:字典名[key]
好了, 这次就讲到这里, 下次我们继续~~