NSArray与NSSet .
NSArray与NSSet
NSArray:有序的集合,存储的元素在一个整块的内存中并按序排列;
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内的对象
循环使用整个NSArray内的对象是非常常用的了,而且最近我在研究究竟怎么能方便的把NSArray存入Core Data,所以这更是必要了,看看如下的方法吧:
1,Objective-C 2.0法,最应该使用的
NSArray *aArray; //我们的Array,假设他已经初始化有内容了 for(id innerObj in aArray) //id可以由其他对象类型替代{//也就是说, 在循环中的参数innerObj就是aArray中的对象//由于NSArray中只能储存对象,所以我们使用id作为内涵对象的类型,其实就是个指针了//而且如果NSArray中的内容不一致,id更不会出现冲突}
for(int i=0;i<[aArray count]; i++){[[aArray objectAtIndex:i] 然后作你想作的事情]; //做爱 作的事情...}
NSEnumerator* myIterator = [myArray reverseObjectEnumerator]; id anObject; while( anObject = [myIterator nextObject]) //每次读取“逐读器”的下一个对象 { //anObject和刚才的innerObj是一个意思,但是看看多了这么多行 }
+ (id)arrayWithObjects:(id)firstObject, ...; // nil terminated - (int)count;- (id)objectAtIndex:(int)index; // NSString *s1=[[myarray objectAtIndex: 0];- (void)makeObjectsPerformSelector:(SEL)aSelector;- (NSArray *)sortedArrayUsingSelector:(SEL)aSelector;- (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];}@endOther convenient create with methods (all return autoreleased objects):[NSString stringWithFormat:@“Meaning of %@ is %d”, @“life”, 42];[NSDictionary dictionaryWithObjectsAndKeys:ankush, @“TA”, janestudent, @“Student”, nil];[NSArray arrayWithContentsOfFile:(NSString *)path];
//NSArray *array = [[NSArray alloc] initWithObjects: @"One",@"Two",@"Three",@"Four",nil]; self.dataArray = array; [array release]; //- (unsigned) Count;数组所包含对象个数; NSLog(@"self.dataArray cound:%d",[self.dataArray count]); //- (id) objectAtIndex: (unsigned int) index;获取指定索引处的对象; NSLog(@"self.dataArray cound 2:%@",[self.dataArray objectAtIndex:2]);
//arrayWithArray: //NSArray *array1 = [[NSArray alloc] init]; NSMutableArray *MutableArray = [[NSMutableArray alloc] init]; NSArray *array = [NSArray arrayWithObjects: @"a",@"b",@"c",nil]; NSLog(@"array:%@",array); MutableArray = [NSMutableArray arrayWithArray:array]; NSLog(@"MutableArray:%@",MutableArray); array1 = [NSArray arrayWithArray:array]; NSLog(@"array1:%@",array1); //Copy //id obj; NSMutableArray *newArray = [[NSMutableArray alloc] init]; NSArray *oldArray = [NSArray arrayWithObjects: @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil]; NSLog(@"oldArray:%@",oldArray); for(int i = 0; i < [oldArray count]; i++) { obj = [[oldArray objectAtIndex:i] copy]; [newArray addObject: obj]; } // NSLog(@"newArray:%@", newArray); [newArray release];//快速枚举 //NSMutableArray *newArray = [[NSMutableArray alloc] init]; NSArray *oldArray = [NSArray arrayWithObjects: @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil]; NSLog(@"oldArray:%@",oldArray); for(id obj in oldArray) { [newArray addObject: obj]; } // NSLog(@"newArray:%@", newArray); [newArray release]; //Deep copy //NSMutableArray *newArray = [[NSMutableArray alloc] init]; NSArray *oldArray = [NSArray arrayWithObjects: @"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",nil]; NSLog(@"oldArray:%@",oldArray); newArray = (NSMutableArray*)CFPropertyListCreateDeepCopy(kCFAllocatorDefault, (CFPropertyListRef)oldArray, kCFPropertyListMutableContainers); NSLog(@"newArray:%@", newArray); [newArray release]; //Copy and sort //NSMutableArray *newArray = [[NSMutableArray alloc] init]; NSArray *oldArray = [NSArray arrayWithObjects:
- (void)addObject:(id)anObject;- (void)insertObject:(id)anObject atIndex:(int)index;- (void)removeObjectAtIndex:(int)index;- (void)removeAllObjects;
//NSArray *array; array = [NSMutableArray arrayWithCapacity:20];
//- (void) addObject: (id) anObject; //NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil]; [array addObject:@"Four"]; NSLog(@"array:%@",array);
//-(void) removeObjectAtIndex: (unsigned) index; //NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil]; [array removeObjectAtIndex:1]; NSLog(@"array:%@",array);
//1、- (NSEnumerator *)objectEnumerator; //从前向后 //NSMutableArray *array = [NSMutableArray arrayWithObjects:@"One",@"Two",@"Three",nil]; NSEnumerator *enumerator; enumerator = [array objectEnumerator]; id thingie; while (thingie = [enumerator nextObject]) { NSLog(@"thingie:%@",thingie); }//2、- (NSEnumerator *)reverseObjectEnumerator; //从后向前 //NSMutableArray *array = [NSMutableArray arrayWithObjects: @"One",@"Two",@"Three",nil]; NSEnumerator *enumerator; enumerator = [array reverseObjectEnumerator]; id object; while (object = [enumerator nextObject]) { NSLog(@"object:%@",object); }//3、快速枚举 //NSMutableArray *array = [NSMutableArray arrayWithObjects: @"One",@"Two",@"Three",nil]; for(NSString *string in array) { NSLog(@"string:%@",string); }
//将NSRect放入NSArray中 NSMutableArray *array = [[NSMutableArray alloc] init]; NSValue *value; CGRect rect = CGRectMake(0, 0, 320, 480); value = [NSValue valueWithBytes:&rect objCType:@encode(CGRect)]; [array addObject:value]; NSLog(@"array:%@",array);//从Array中 提取 value = [array objectAtIndex:0]; [value getValue:&rect]; NSLog(@"value:%@",value);
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,同样也是2NSLog(@"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];
+ setWithObjects:(id)firstObj, ...; // nil terminated- (int)count;- (BOOL)containsObject:(id)anObject;- (id)anyObject;- (void)makeObjectsPerformSelector:(SEL)aSelector;- (id)member:(id)anObject; // uses isEqual: and returns a matching object (if any)
+ (NSMutableSet *)set;- (void)addObject:(id)anObject;- (void)removeObject:(id)anObject;- (void)removeAllObjects;- (void)unionSet:(NSSet *)otherSet;- (void)minusSet:(NSSet *)otherSet;- (void)intersectSet:(NSSet *)otherSet;
- (NSUInteger)hash & - (BOOL)isEqual:(NSObject *)objImportant methods:+ dictionaryWithObjectsAndKeys: (id)firstObject, ...;- (int)count;- (id)objectForKey:(id)key;- (NSArray *)allKeys;- (NSArray *)allValues; -----创建字典 ----- //- (id) initWithObjectsAndKeys; //NSDictionary *dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:@"One",@"1",@"Two",@"2",@"Three",@"3",nil]; NSString *string = [dictionary objectForKey:@"One"]; NSLog(@"string:%@",string); NSLog(@"dictionary:%@",dictionary); [dictionary release];NSMutableDictionaryMutable version of NSDictionary.+ (NSMutableDictionary *)dictionary;- (void)setObject:(id)anObject forKey:(id)key;- (void)removeObjectForKey:(id)key;- (void)removeAllObjects;- (void)addEntriesFromDictionary:(NSDictionary *)otherDictionary;-----创建可变字典 -----
//创建 NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; //添加字典 [dictionary setObject:@"One" forKey:@"1"]; [dictionary setObject:@"Two" forKey:@"2"]; [dictionary setObject:@"Three" forKey:@"3"]; [dictionary setObject:@"Four" forKey:@"4"]; NSLog(@"dictionary:%@",dictionary);//删除指定的字典 [dictionary removeObjectForKey:@"3"]; NSLog(@"dictionary:%@",dictionary);