一、集合(Collection)
collection(集合)能容纳多个事物的容器就是集合
在ios中的集合容器有数组、字典、集合
二、分类
1、数组
NSArray,不可变数组,可以存放多个对象,实例化的时候以nil表示结束,通过下标索引对象
NSArray无法添加、删除、修改元素
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
//集合对象
//实例化数组对象
NSArray *array = [[NSArray alloc]initWithObjects:@"a",@"b",@"c", nil];
NSLog(@"%@",array);
//获取数组内的某个元素
NSString *theLastItem = [array lastObject];
NSLog(@"数组内的最后一个元素是:%@",theLastItem);
//获取数组内的其他元素
NSString *theOtherItem = [array objectAtIndex:0];
NSLog(@"数组内的第一个元素是:%@",theOtherItem);
NSArray *anyArray = [array arrayByAddingObject:@"d"];
NSLog(@"%@",array);
NSLog(@"%@",anyArray);
return 0;
}
NSMutableArray可变数组,是NSArray的子类,相比NSArray增加了添加、删除、修改功能
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
//可变数组的对象
NSMutableArray *mutableArray = [[NSMutableArray alloc]init];
[mutableArray addObject:@"1"];
[mutableArray addObject:@"2"];
NSLog(@"%@",mutableArray);
[mutableArray addObject:@"3"];
NSLog(@"%@",mutableArray);
return 0;
}
2、字典
NSDictionary,不可变字典,存放多个键值对(key-value),最后以nil结束,字典中的对象存储没有顺序,使用key来索引每个对象
NSDictionary不可添加、删除、修改元素
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
//实例化不可变字典对象
NSDictionary *dictionary = [[NSDictionary alloc]initWithObjectsAndKeys:@"value_1",@"key_1",@"value_2",@"key_2", nil];
NSLog(@"%@",dictionary);
//在实例化不可变字典对象时,如果在参数的前边已经包含了某一个关键字key,那么在后边出现的相同的关键字则不会添加到字典容器内。
//获取字典中的某个值
NSString *string = [dictionary objectForKey:@"key_1"];
NSLog(@"%@",string);
return 0;
}
NSMutableDictionary可变字典,是NSDictionary的子类,相比NSDictionary增加了添加、删除、修改方法
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
//实例化可变字典对象
NSMutableDictionary *mutableDictionary = [[NSMutableDictionary alloc]init];
[mutableDictionary setObject:@"1" forKey:@"a"];
[mutableDictionary setObject:@"2" forKey:@"b"];
NSLog(@"%@",mutableDictionary);
[mutableDictionary setObject:@"3" forKey:@"a"];
NSLog(@"%@",mutableDictionary);
//在对可变字典对象添加元素时,如果在字典对象内已经包含了对应的关键字,那么可变字典对象内的该关键字的值则会发生变化
return 0;
}
3、集合
NSSet,不可变集合,可以存放多个对象,以nil表示结束,对象在set中存储是无序的
不可以添加、删除、修改元素
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
//实例化不可变集合对象
NSSet *set = [[NSSet alloc] initWithObjects:@"1",@"2",@"1", nil];
NSLog(@"%@",set);
//只能打印1、2,说明集合里面是不允许出现重复对象的
//而且不能获取集合中的某一个对象,只能获取集合内的任意一对象
NSString *item = [set anyObject];
NSLog(@"item:%@",item);
return 0;
}
NSMutableSet,可变集合,是NSSet的子类,相比NSSet增加了添加、删除、修改的方法
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
//实例化可变集合对象
NSMutableSet *mutableSet = [[NSMutableSet alloc]init];
[mutableSet addObject:@"1"];
[mutableSet addObject:@"2"];
NSLog(@"%@",mutableSet);
[mutableSet addObject:@"1"];
NSLog(@"%@",mutableSet);
//可变集合里面也是不允许出现重复对象的
return 0;
}
三、快速枚举
for…in语法,OC2.0的新语法。简化了传统的for循环
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
//实例化数组对象
NSMutableArray *mutable = [[NSMutableArray alloc] init];
[mutable addObject:@"abc"];
[mutable addObject:@"qwe"];
[mutable addObject:@"ert"];
for (NSInteger index = 0; index < [mutable count]; index++) {
NSString * stringItem = [mutable objectAtIndex:index];
NSLog(@"%d:%@",index,stringItem);
}
for (NSString *item in mutable){
NSLog(@"%@",item);
}
return 0;
}
四、collection的内存管理
collection会对自己内部的元素负责。
元素添加到collection的时候,元素会自动retain,当元素remove的时候,元素会自动release。
collection释放的时候,会先让内部元素relesae,然后自己再release。
五、总结
官方很多类都分可变和不可变,无论类名是什么,可变类通常比不可变类多了增删改等功能。
数组以下标来索引数据
字典以key来索引数据
集合是无序的,随机取数据
六、术语与技巧
字符串比较特殊:除了系统给了NSString和NSMutableString,还有一个常量字符串(无此类)
常量字符串的引用计数不能作为衡量内存的标准,%d打印会是-1,%u打印会是正的最大值
collection自身会管理内存,元素添加(含初始化)到collection,collection会调用对象的retain方法,当collection销毁或者元素移除的时候,会调用对象的release方法。