=================== NSArray ====================(不可变数组,一旦建立就不能被更改)
Ordered collection of objects. Immutable(you cannot add or remove objects to it once it’s created)
Important methods:
+ (id)arrayWithObjects:(id)firstObject, ...; // nil terminated
- (int)count; // 得到array中的对象个数
- (id)objectAtIndex:(int)index; // 得到索引为i的对象
- (BOOL)containsObject:(id)anObject; // 当anObject出现在array中,则返回yes(实际是通过isEqual:方法来判断)
- (unsigned)indexOfObject:(id)anObject; // 查找array中的anObject,并返回其最小索引值。没找到返回NSNotFound.
- (void)makeObjectsPerformSelector:(SEL)aSelector;
- (NSArray *)sortedArrayUsingSelector:(SEL)aSelector;
- (id)lastObject; // 得到array中最后一个对象。如果array中没有任何对象存在,则返回nil
注:
类方法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 dictionaryWithObjectsAndKeys:ankush, @“TA”, janestudent, @“Student”, nil];
[NSArray arrayWithContentsOfFile:(NSString *)path];
-----创建数组 -----
NSArray *array = [[NSArray alloc] initWithObjects:@"One",@"Two",@"Three",@"Four",nil];
self.dataArray = array;
NSLog(@"self.dataArray count is:%d",[self.dataArray count]);
NSLog(@"self.dataArray index 2 is:%@",[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);
//快速枚举
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);
//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);
=================== NSMutableArray ====================(可变数组)
Mutable version of NSArray.
- (void)addObject:(id)anObject; // 在array最后添加anObject, 添加nil是非法的.
- (void)addObjectsFromArray:(NSArray *)otherArray; //在array最后把otherArray中的对象依次添加进去。
- (void)insertObject:(id)anObject atIndex:(int)index; //在索引index处插入anObject, 若index被占用,会把之后的object向后移。
- (void)removeObjectAtIndex:(int)index; //删除index处的对象,后面的对象依次向前移。
- (void)removeObject:(id)anObject; // 删除所有和anObject相等的对象,同样使用isEqual:作为相等比较方法.
- (void)removeAllObjects;
注:我们不能把nil加到array中。但有时候我们真想给array加一个空的对象,可以使用NSNull来做这件事。如:
[myArray addObject:[NSNull null]];
-----给数组分配容量-----
//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);
}
----- NSValue(对任何对象进行包装)-----
//将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);
//同样做数组替换时
[objectsArray replaceObjectAtIndex:0 withObject:p2];
//执行清空数组
[objectsArray removeAllObjects];