NSSet:无序的集合,散列存储。
读developer.apple关于NSSet的解释:You can use sets as an alternative to arrays when the order of elements isn’t important and performance in testing whether an object is contained in the set is a consideration—while arrays are ordered, testing for membership is slower than with sets.
就是说,如果搜索一个元素,NSSet的效率会比NSArray高。为什么呢?道理比较简单:hash!NSSet中元素的存储和访问都是一个hash的过程。比如你要存储元素A,一个hash算法直接就能直接找到A应该存储的位置;同样,当你要访问A时,一个hash过程就能找到A存储的位置。而对于NSArray,若想知道A到底在不在数组中,则需要一个一个元素比较,显然效率没了。
循环使用整个NSArray内的对象
1,Objective-C 2.0法,最应该使用的
2,C的老方法,不推荐,低性能
3, 用NSEnumerator(不知道怎么翻译阿….)
三种集合类来收集cocoa对象(NSObject对象):
NSArray
NSSet
NSDictionary用于键值映射
以上三种集合类是不可变的(一旦初始化后,就不能改变)
以下是对应的三种可变集合类(这三种可变集合类是对应上面三种集合类的子类):
NSMutableArray
NSMutableSet
NSMutableDictionary
注:这些集合类只能收集cocoa对象(NSOjbect对象),如果想保存一些原始的C数据(例如,int, float, double, BOOL等),则需要将这些原始的C数据封装成NSNumber类型的,NSNumber对象是cocoa对象,可以被保存在集合类中。
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
@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];
-----创建数组
------
//快速枚举
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;
-----
//将NSRect放入NSArray中
----★使用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.
- (NSUInteger)hash & - (BOOL)isEqual:(NSObject *)obj
Important methods:
+ dictionaryWithObjectsAnd
- (id)objectForKey:(id)key;
- (NSArray *)allKeys;
- (NSArray *)allValues;
Mutable version of NSDictionary.
+ (NSMutableDictionary *)dictionary;
- (void)removeObjectForKey:(id)key;
- (void)addEntriesFromDictionary
//创建
//添加字典