三种集合类来收集cocoa对象(NSObject对象):
NSArray 用于对象有序集合(相当于是数组)
NSSet 用于对象无序集合
NSDictionary用于键值映射
以上三种集合类是不可变的(一旦初始化后,就不能改变)
以下是对应的三种可变集合类(这三种可变集合类是对应上面三种集合类的子类):
NSMutableArray
NSMutableSet
NSMutableDictionary
注:这些集合类只能收集cocoa对象(NSOjbect对象),如果想保存一些原始的C数据(例如,int, float, double, BOOL等),则需要将这些原始的C数据封装成NSNumber类型的,NSNumber对象是cocoa对象,可以被保存在集合类中。
使 NSDictionary 支持NSMutableCopy协议,该协议返回一个 NSMutableDictionary
(这个方法创建的是浅副本,即该新对象拥有源字典所有的对象,它们并不是副本,而是相同的对象)。
NSArray
Ordered collection of objects.
Important methods:
+ (id)arrayWithObjects:(id)firstObject, ...;
- (int)count;
- (id)objectAtIndex:(int)index;
- (void)makeObjectsPerformSelect
- (NSArray *)sortedArrayUsingSelector
- (id)lastObject; // returns nil if there are no objects in the array (convenient)
注:
类方法arrayWithObjects 可以创建an autoreleased NSArray of the items.例如
@implementation MyObject
- (NSArray *)coolCats
return [NSArray arrayWithObjects:@“Steve”, @“Ankush”, @“Sean”, nil];
}
@end
Other convenient create with methods (all return autoreleased objects):
[NSString stringWithFormat:@“Meaning of %@ is %d”, @“life”, 42];
[NSDictionary dictionaryWithObjectsAnd
[NSArray arrayWithContentsOfFile:(NSString *)path];
= array;
(unsigned) Count;数组所包含对象个数;
(id) objectAtIndex: (unsigned int) index;获取指定索引处的对象;
------ 从一个数组拷贝数据到另一数组(可变数级) -------
= [NSArray arrayWithArray:array];
obj;
//快速枚举
*newArray = [[NSMutableArray alloc] init];
obj in oldArray)
copy
*newArray = [[NSMutableArray alloc] init];
and sort
*newArray = [[NSMutableArray alloc] init];
NSMutableArray
Mutable version of NSArray.
- (void)addObject:(id)anObject;
- (void)insertObject:(id)anObject atIndex:(int)index;
- (void)removeObjectAtIndex:(int)index;
- (void)removeAllObjects;
-----给数组分配容量-----
-----在数组末尾添加对象-----
-----删除数组中指定索引处对象-----
//1、- (NSEnumerator *)objectEnumerator;
thingie;
(NSEnumerator *)reverseObjectEnumerator;
object;
-----
提取
----★使用NSMutableArray要防止内存泄露★------
NSObject* p1 = [[NSObject alloc] init];
NSObject* p2 = [[NSObject alloc] init];
NSMutableArray* objectsArray = [[NSMutableArray alloc] init];
[objectsArray
addObject:p1];
NSLog(@"p1 count:%d", [p1 retainCount]);//输出 2,也就是执行追加对象后,对象的计数器也被加1
//[p1 release];
//NSLog(@"p1 count:%d", [p1 retainCount]);
//同样做数组替换时
[objectsArray replaceObjectAtIndex:0 withObject:p2];
NSLog(@"p2 count:%d", [p2 retainCount]);//输出 2,同样也是2
NSLog(@"p1 count:%d", [p1 retainCount]);//输出 1,对象p1仍然存在
//[p2 release];
//NSLog(@"p2 count:%d", [p2 retainCount]);
//执行清空数组
[objectsArray removeAllObjects];
NSLog(@"p2 count:%d", [p2 retainCount]);//输出 1,对象p2仍然存在
//[p2 release];
由此可知,每次执行上面的数组操作后,要执行对象release,如上面注释中的语句,才能保证内存不泄露。
NSSet
Unordered collection of objects.
Immutable. You cannot add or remove objects to it once it’s created.
Important methods:
+ setWithObjects:(id)firstObj, ...;
- (BOOL)containsObject:(id)anObject;
- (id)anyObject;
- (void)makeObjectsPerformSelect
- (id)member:(id)anObject; // uses isEqual: and returns a matching object (if any)
NSMutableSet
Mutable version of NSSet.
+ (NSMutableSet *)set;
- (void)removeObject:(id)anObject;
- (void)unionSet:(NSSet *)otherSet;
- (void)minusSet:(NSSet *)otherSet;
- (void)intersectSet:(NSSet *)otherSet;
NSDictionary
key-value, key-value, ..... 一系列键值对。
Immutable. You cannot add or remove objects to it once it’s created.
Keys are objects which must implement.
查询字典的值:objectForKey
NSMutableDictionary的dictionary方法可以创建一个可变字典,也可以使用dictionaryWithCapaticy:。
使用 setObject:forkey: 方法添加字典元素,如果关键字已存在,则用新植替换旧值。
类似的,NSMutableDictionary类允许随意添加或删除字典元素。
添加元素:setObject:forkey:
删除元素:removeObjectForKey:
- (NSUInteger)hash & - (BOOL)isEqual:(NSObject *)obj
Important methods:
+ dictionaryWithObjectsAnd
- (id)objectForKey:(id)key;
- (NSArray *)allKeys;
- (NSArray *)allValues;
*dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil];
NSMutableDictionary
Mutable version of NSDictionary.
+ (NSMutableDictionary *)dictionary;
- (void)removeObjectForKey:(id)key;
- (void)addEntriesFromDictionary
//创建
//添加字典