Foundation Framework (3)
NSArray, NSDictionary, NSSet这三个集合类,是不可变的,一旦它们被创建了,那就不能对它们进行增删改操作。
而实际情况是,我们需要动态的集合,随着程序的运行我们需要反复增删改集合里的元素,所以Foundation Framework提高了这三个集合的对应可变版本。
固定版本 | 可变版本 |
---|---|
NSArray | NSMutableArray |
NSDictionary | NSMutableDictionary |
NSSet | NSMutableSet |
可变版本在对应集合类的基础上多了增加,删除,修改的方法。
集合常见的用法
遍历,查询,访问
可变集合还多出:增加,删除,修改
遍历
NSArray *array = @[@1, @2, @3, @4];
for (NSUInteger i = 0; i < [array count]; i++] {
NSLog(@"object at index %lu is %@", i, array[i]);
}
OC还提供了快速迭代语法
- 数组的快速遍历
NSArray *array = @[@1, @2, @3, @4];
for (NSNumber *element in array) {
NSLog(@"Element:%@", element);
}
- NSSet的快速遍历
NSSet *set = [NSSet setWithObjects: @1, @2, @3, @4, nil];
for (NSNumber *element in set) {
NSLog(@"Element: %@", element);
}
- NSDictionary的快速遍历
NSDictionary *dict = @{@"one":@1,
@"two":@2,
@"three":@3,
@"four":@4}
for (NSString *key in dict) {
NSLog(@"key: %@, value: %@", key, dict[key]);
}
//字典类for循环里是遍历key
查询
- NSArray与NSSet的查询
NSArray *array = @[@1, @2, @3, @4];
NSSet *set = [NSSet setWithObjects:@1, @2, @3, @4, nil];
BOOL isArrayContained = [array containsObject:@1];
BOOL isSetContained = [set containsObject:@2];
注意:虽然NSArray跟NSSet的`containsObject:`方法差不多,但是但元素多的时候,
还是使用NSSet来查询效率比较高
- NSDictionary的查询
NSDictionary *dict = @{@"one", @1,
@"two", @2,
@"three",@3};
if (dict[@"one"] != nil) {
NSLog(@"key one is contained!");
} else {
NSLog(@"key one is not contained!");
}
访问
- NSArray的访问
NSArray *array = @[@1, @2, @3, @4];
NSNumber *elem1 = [array objectAtIndex:2];
NSNumber *elem2 = array[2];
- NSDictionary的访问
NSDictionary *dict = @[@"one", @1,
@"two", @2,
@"three", @3,
@"four": @4};
NSNumber *elem1 = [dict objectForKey:@"one"];
NSNumber *elem2 = dict[@"one"];
- NSSet的访问
NSSet *set = [NSSet setWithObjects:@1, @2, @3, @4, nil];
NSNumber *elem = [set member:@1];
可变集合类
添加元素
- NSMutableArray 添加元素
NSMutable *array = [NSMutableArray arryaWithObjects:@1, @2, @3, nil];
[array addObject:@5];
[array insertObject:@3 atIndex:2];
- NSMutableDictionary 添加元素
NSMutableDictionary *dict = [@{@"one", @1,
@"two", @2,
@"three", @3,
@"four", @4}mutableCopy];
[dict setObject:@5 forKey:@"five"];
//因为字典类内元素本来就无序的,所以没有中间插入一说
dict[@"five"] = @5;
//更简洁的写法
- NSMutableSet 添加元素
NSMutableSet *set = [NSMutableSet setWithObjects:@1, @2, @3, nil];
[set addObject:@4];
//如果是添加已有元素,由于集合类不能有重复元素,这时就什么都不做
删除元素
- 删除NSMutableArray元素
NSMutableArray *array = [NSMutable arrayWithObjects:@1, @2, @3, @4, nil];
[array removeLastObject];
[array removeObjectAtIndex:1];
- 删除NSMutableDictionary元素
NSMutableDictionary *dict = [@{@"one": @1,
@"two": @2,
@"three":@3} mutableCopy];
[dict removeObjectForKey:@"one"];
- 删除NSMutableSet元素
NSMutableSet *set = [NSMutableSet setWithObjects:@1, @2, @3, nil];
[set removeObject:@2];
修改元素
- 修改NSMutableArray元素
NSMutableArray *array = [NSMutableArray arrayWithObjects:@1, @2, @3, @4,nil];
[array replaceObjectAtIndex:2 withObject:@5];
array[2] = @5;//更简洁的写法
- 修改NSMutableDictionary元素
跟添加元素的语法是一样的,被添加的键值如果原集合里已经有,那就代表替换(修改)
NSMutableDictionary *dict = [@{@"one", @1,
@"two", @2,
@"three", @3,
@"four", @4}mutableCopy];
[dict setObject:@8 forKey:@"four"];
//因为字典类内元素本来就无序的,所以没有中间插入一说
dict[@"four"] = @8;
//更简洁的写法
- 修改NSMutableSet元素
NSMutable本来的功能就是增加和删除,并没有修改的功能
通过apple文档查询这三个集合类的API可以学习到更多的用法