[OC Foundation框架 - 7] NSArray的创建与遍历
NSArray是不可变的,不能先创建再添加元素
NSArray可以放入任何OC对象,但不能放入基本数据类型、结构体、枚举等非OC对象
不能存储nil
A.常用方法1
最后的nil用作标示数组的结束,不会被存储进数组元素,不允许在其他地方插入
- 创建
- 返回用量
- 是否含有某元素
- 最后的元素
- 取得某位置的元素
当一个对象放入数组的时候,这个对象的计数器加1
1 #pragma mark create a array 2 void arrayCreate() 3 { 4 //Create an empty array 5 NSArray *array = [NSArray array]; 6 7 //Create an array with one element 8 array = [NSArray arrayWithObject:@"123"]; 9 10 array = [NSArray arrayWithObjects:@"a", @"b", @"d", nil]; 11 12 NSUInteger count =[array count]; 13 NSLog(@"%@", count); 14 15 [array release]; 16 }
最后的nil用作标示数组的结束,不会被存储进数组元素,不允许在其他地方插入
error: array = [NSArray arrayWithObjects:@"a”, nil, @"d"];
1 #pragma mark common use 2 void arrayUser() 3 { 4 NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil]; 5 if ([array containsObject:@"a"]) 6 { 7 NSLog(@"Contans a"); 8 } 9 10 NSString *last = [array lastObject]; 11 NSLog(@"%@", last); 12 13 NSString *str = [array objectAtIndex:1]; 14 NSLog(@"%@", str); 15 16 NSUInteger location = [array indexOfObject:@"c"]; 17 NSLog(@"%@", location); 18 19 [array release]; 20 } 21 22 #pragma memory manage 23 void memoryManage() 24 { 25 Student *stu1 = [[Student alloc] init]; 26 Student *stu2 = [[Student alloc] init]; 27 Student *stu3 = [[Student alloc] init]; 28 29 NSLog(@"stu1: %zi", [stu1 retainCount]); 30 31 NSArray *array = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil]; 32 33 NSLog(@"stu1: %zi", [stu1 retainCount]); 34 35 [stu1 release]; 36 [stu2 release]; 37 [stu3 release]; 38 39 [array release]; 40 41 }
B.常用方法2
1.给数组所有元素发送消息,调用同一个方法
1 void arrayMessage() 2 { 3 Student *stu1 = [[Student alloc] init]; 4 Student *stu2 = [[Student alloc] init]; 5 Student *stu3 = [[Student alloc] init]; 6 7 NSArray *array = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil]; 8 9 [array makeObjectsPerformSelector:@selector(test2:) withObject:@"test2"]; 10 11 [stu1 release]; 12 [stu2 release]; 13 [stu3 release]; 14 15 [array release]; 16 }
2.遍历
(1)for 循坏
1 void arrayLoop() 2 { 3 Student *stu = [[Student alloc]init]; 4 NSArray *array = [[NSArray alloc] initWithObjects:stu, @"2", @"3", nil]; 5 unsigned long count = array.count; 6 // for (int i=0; i<count; i++) 7 // { 8 // id obj = [array objectAtIndex:i]; 9 // NSLog(@"%i - %@", i, obj); 10 // } 11 12 int i = 0; 13 for (id obj in array) 14 { 15 NSLog(@"%i - %@", i, obj); 16 i++; 17 } 18 19 [stu release]; 20 [array release]; 21 }
(2)使用Block进行循环处理
1 void arrayLoopByBlock() 2 { 3 Student *stu = [[Student alloc]init]; 4 NSArray *array = [[NSArray alloc] initWithObjects:stu, @"2", @"3", nil]; 5 6 7 [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) { 8 NSLog(@"%zi - %@", idx, obj); 9 if (idx == 1) 10 { 11 *stop = YES; 12 } 13 }]; 14 15 [stu release]; 16 [array release]; 17 }
(3).迭代器
1 void arrayLoopByEnumerator() 2 { 3 Student *stu = [[Student alloc]init]; 4 NSArray *array = [[NSArray alloc] initWithObjects:stu, @"2", @"3", nil]; 5 6 // NSEnumerator *e = [array objectEnumerator]; 7 NSEnumerator *e = [array reverseObjectEnumerator]; 8 id obj = nil; 9 while (obj = [e nextObject]) 10 { 11 NSLog(@"The element is %@", obj); 12 } 13 14 [stu release]; 15 [array release]; 16 }
创建整数数组
(1)
1 NSArray *array61 = [NSArray arrayWithObjects:@0, @1, @2, @3, @4, @5, @6, @7, @8, @9, nil];
实际上是把数字自动转化成了NSNumber类型
(2)不能用于NSMutableArray
1 NSArray *array61 = @[@0, @1, @2, @3, @4, @5, @6, @7, @8, @9];