一.集合家族
1.NSArray
NSArray是一个Cocoa类,用来存储对象的有序列表,
限制:a.只能存储Objective-C的对象,而不能存储C语言中的基本数据类型,如int,float,enum,struct或者NSArray中的随机指针。
b.不能在NSArray中存储nil(对象的零值或NULL值)。
//通过类方法arrayWithObjects创建一个新的NSArray,发送一个以逗号分隔的对象列表,
//在列表结尾添加nil代表列表结束(这就是不能在数组中存储nil的原因之一)
NSArray *array;
array = [NSArray arrayWithObjects:@"one",@"two",@"three",nil];
//获得对象的个数
- (unsigned) count;
//获取特定索引处的对象
- (id)objectAtIndex: (unsigned int) index;
2.NSMutableArray:可变数组
//数组容易也只是数组最终大小的一个参考,不会将对象预写入数组,也不会用该容量值来限制数组的大小。
+ (id) arrayWithCapactity: (unsigned int) numItems;
//在数组结尾处添加对象
- (void) addObject: (id) anObject
//删除特定索引处的对象
- (void)removeObjectAtIndex:(unsigned) index;
3.NSEnumerator(枚举)
//创建
- (NSEnumerator *) objectNSEnumeratort;
NSEnumerator *anumerator;
enumerator = [array objectNSEnumerator];
//从后向前浏览集合
- (void)reverseObjectEnumerator;
//获得枚举器之后可以开始一个while循环,每次循环都向这个枚举器请求它的下一个对象;
- (id) nextObject;
nextObject返回nil值时,循环结束
整个循环代码:
NSenumerator *enumerator;
enumerator = [array objectEnumerator];
id thingie;
//对可变数组进行枚举操作时,不能对数组容器进行添加或删除操作
while(thingie == [enumerator nextObject]){
NSLog(@"I found %@",thingie);
}
4.快速枚举
for(NSString *string in array){
NSLog(@"I found %@",string);
}
5.NSDictionary(散列结构)
(1)NSDictionary
//创建字典
+ (id)dicationaryWithObjectsAndKeys:
(id)firstObject,...;
//获取字典中的值
- (id) objectForKey: (id) aKey;
示例代码:
Tire *t1 = [Tire new];
Tire *t2 = [Tire new];
Tire *t3 = [Tire new];
Tire *t4 = [Tire new];
NSDictionary *tires;
tires = [NSDictionary dictionaryWithObjectsAndKeys:
t1,@"front-left",t2,@"front-right",
t3,@"back-left",t4,@"back-right",nil]
Tire *tire =[tires objectForKey: @"back-right"];
(2)NSDictionary不可变,NSMutableDictionary允许随意添加或删除元素。
//创建NSMutableDictionary可以发送dictionary消息,也可以用dictionaryWithCapacity:方法
//并用告诉该字典的最终大小
+ (id) dicationaryWithCapacity: (unsigned int) numItems;//大小仅仅是建议,不是对其限制
//添加元素
- (void) setObject: (id) anObject
forKey: (id) aKey;
//删除元素
- (void) removeObjectForKey:(id)aKey;
基本数据类型包装类
1.NSNumber
NSArray和NSDictionary只能存储对象,而不能直接存储任何基本类型的数据,但是可以用对象来封装然后添加到集合中
Cocoa提供了NSNumber类来包装(即以对象形式实现)基本数据类型
a.几个方法示例:
+ (NSNumber *) numberWithChar : (Char)value;
+ (NSNumber *) numberWithInt : (Int)value;
+ (NSNumber *) numberWithFloat : (Float)value;
+ (NSNumber *) numberWithBool: (Bool)value;
b。Objective-c中不支持自动装箱
NSNumber *number;
number = [NSNumber numberWithInd:42];
[array addObject: number];
[dictionary setObject: number forkey: @"Bork"];
C。只要将一个基本类型数据封装到NSNumber中就可以通过下列方法重新获取它
- (Char) charValue;
- (Int) intValue;
- (Float) floatValue;
- (Bool) boolValue;
- (NSString *) stringValue
2.NSValue
NSNumber是NSValue的子类,NSValue可以包装任意值。
+ (NSValue *) valueWithBytes: (const void *) value
objCType: (const void *) type;
可以使用类方法创建新的NSValue:
3.NSNull
不能将nil存入近NSArray,NSDictionary中,因为nil代表的是一个集合的结束。
传空值用如下方法:
+ (NSNull *) null;
示例:
[contact setObject: [NSNull null] forKey:@"home fax machine"];
访问它的方法
id homefax;
homefax = [contact objectForKey: @"home fax machine"];
if(homefax == [NSNull null]){
//no fax machine......
}
二.集合综合运用小例子查找文件
int main(int argc, Const char *argc[]){
//初始化对象池
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
//获得fileManager
NSFileManager *defaultManager;
defaultManager = [NSFileManager defaultManager];
//将路径封装
NSString *home;
home = [@"~",stringByExpandingTildeInPath];
//将路径字符串传递给文件管理器
NSDirectoryEnumerator *direnum;
direnum = [manager enumeratorAtPath: home];
//创建可变数组
NSMutableArray *files;
files = [NSMutableArray arrayWithCapacity: 42];
//迭代符合条件扩展名为jpg的文件名
NSString *filename;
while(filename = [direnum nextObject]){
if([[filename pathExtension] isEqualTo: @".jpg"]){
[files addObject: fillname];
}
}
//打印所有符合条件的文件名
NSEumerator *filenum;
filenum = [files objectEnumerator];
while(filename = [filenum nextObject]){
NSLog(@"%@",filename);
}
[pool drain];
return 0;
}
快速迭代方法
int main(int argc, Const char *argc[]){
NSAutoreleasePool *pool;
pool = [[NSAutoreleasePool alloc] init];
NSFileManager *defaultManager;
defaultManager = [NSFileManager defaultManager];
NSString *home;
home=[@"~",stringExpandingTildeInPath];
NSMutableArray *files;
files = [NSMutableArray arrayWithCapacity: 42];
for(NSString *filename in [defaultManager enumeratorAtPath: home]){
if([[filename pathExtension] isEqualTo: @".jpg"]){
[files addObject filename];
}
}
for(NSString *filename in files){
NSLog(@"@",filename);
}
}