[OC Foundation框架 - 7] NSArray的创建与遍历

NSArray是不可变的,不能先创建再添加元素

NSArray可以放入任何OC对象,但不能放入基本数据类型、结构体、枚举等非OC对象
不能存储nil
 
A.常用方法1
  1. 创建
  2. 返回用量
  3. 是否含有某元素
  4. 最后的元素
  5. 取得某位置的元素
当一个对象放入数组的时候,这个对象的计数器加1
 #pragma mark create a array
void arrayCreate()
{
//Create an empty array
NSArray *array = [NSArray array]; //Create an array with one element
array = [NSArray arrayWithObject:@""]; array = [NSArray arrayWithObjects:@"a", @"b", @"d", nil]; NSUInteger count =[array count];
NSLog(@"%@", count); [array release];
}

最后的nil用作标示数组的结束,不会被存储进数组元素,不允许在其他地方插入

error:    array = [NSArray arrayWithObjects:@"a”, nil, @"d"];
 #pragma mark common use
void arrayUser()
{
NSArray *array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];
if ([array containsObject:@"a"])
{
NSLog(@"Contans a");
} NSString *last = [array lastObject];
NSLog(@"%@", last); NSString *str = [array objectAtIndex:];
NSLog(@"%@", str); NSUInteger location = [array indexOfObject:@"c"];
NSLog(@"%@", location); [array release];
} #pragma memory manage
void memoryManage()
{
Student *stu1 = [[Student alloc] init];
Student *stu2 = [[Student alloc] init];
Student *stu3 = [[Student alloc] init]; NSLog(@"stu1: %zi", [stu1 retainCount]); NSArray *array = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil]; NSLog(@"stu1: %zi", [stu1 retainCount]); [stu1 release];
[stu2 release];
[stu3 release]; [array release]; }
 
B.常用方法2
 
1.给数组所有元素发送消息,调用同一个方法
 void arrayMessage()
{
Student *stu1 = [[Student alloc] init];
Student *stu2 = [[Student alloc] init];
Student *stu3 = [[Student alloc] init]; NSArray *array = [[NSArray alloc] initWithObjects:stu1, stu2, stu3, nil]; [array makeObjectsPerformSelector:@selector(test2:) withObject:@"test2"]; [stu1 release];
[stu2 release];
[stu3 release]; [array release];
}
 
2.遍历
(1)for 循坏
 void arrayLoop()
{
Student *stu = [[Student alloc]init];
NSArray *array = [[NSArray alloc] initWithObjects:stu, @"", @"", nil];
unsigned long count = array.count;
// for (int i=0; i<count; i++)
// {
// id obj = [array objectAtIndex:i];
// NSLog(@"%i - %@", i, obj);
// } int i = ;
for (id obj in array)
{
NSLog(@"%i - %@", i, obj);
i++;
} [stu release];
[array release];
}
 
(2)使用Block进行循环处理
 void arrayLoopByBlock()
{
Student *stu = [[Student alloc]init];
NSArray *array = [[NSArray alloc] initWithObjects:stu, @"", @"", nil]; [array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%zi - %@", idx, obj);
if (idx == )
{
*stop = YES;
}
}]; [stu release];
[array release];
}
 
(3).迭代器
 void arrayLoopByEnumerator()
{
Student *stu = [[Student alloc]init];
NSArray *array = [[NSArray alloc] initWithObjects:stu, @"", @"", nil]; // NSEnumerator *e = [array objectEnumerator];
NSEnumerator *e = [array reverseObjectEnumerator];
id obj = nil;
while (obj = [e nextObject])
{
NSLog(@"The element is %@", obj);
} [stu release];
[array release];
}
创建整数数组
(1)
      NSArray *array61 = [NSArray arrayWithObjects:@, @, @, @, @, @, @, @, @, @, nil];
实际上是把数字自动转化成了NSNumber类型
 
 
(2)不能用于NSMutableArray
 NSArray *array61 = @[@, @, @, @, @, @, @, @, @, @];
 
 
 
上一篇:标签,网页上显示的内容放在这里


下一篇:关于JavaScript中的delete操作