objective-c中的OC_NSArray用法以及数组遍历的3种方法

Person是一个类。

@interface Person:NSObject

@end

@implementation Person

-(NSString*)description

{

return @"this is the person";

}

main函数

Person *p=[[Personalloc]init];

        NSArray *arr=[[NSArrayalloc]initWithObjects:@"one and one",@"two",@"three", p,nil];

        NSArray *arr1=[[NSArrayalloc]initWithObjects:@"one",@"two",p,nil];

        //数组可以装任意对象

        //数组中得每一个对象其实是指向该对象的地址

        NSLog(@"arr:%@",arr);

        NSLog(@"arr1:%@",arr1);

输出结果:

OC_Array[10859:145326] arr:(

    "one and one",

    two,

    three,

    "this is the person"

)

2015-12-14 13:43:49.495 OC_Array[10859:145326]arr1:(

    one,

    two,

    "this is the person"

)

Program ended with exit code: 0


 //数组的遍历

//枚举器遍历法

        NSEnumerator *enumerator=[arr  objectEnumerator];

        id obj;

        while (obj=[enumerator nextObject]) {

            NSLog(@"%@",obj);

        }

输出结果:

OC_Array[11303:151639] one and one

2015-12-14 13:50:08.666 OC_Array[11303:151639] two

2015-12-14 13:50:08.666 OC_Array[11303:151639] three

2015-12-14 13:50:08.666 OC_Array[11303:151639] this is the person

Program ended with exit code: 0

//快速遍历法

        for (obj in arr1) {

            NSLog(@"%@",obj);

        }

        //i遍历法

        NSInteger count =[arr count];

//        id temObj=[arr objectAtIndex:0];

        NSInteger i=0;

        for (i; i<count; ++i) {

            NSLog(@"%@",[arr objectAtIndex:i]);

        }

 //通过已知数组创建新数组

        NSArray *arr2=[[NSArray alloc]initWithArray:arr];

//arr2和arr3的值很相似。如果将NO改为YES则不同,在内存管理机制中会涉及到

 NSArray *arr2=[[NSArray alloc]initWithArray:arr];

 NSArray *arr3=[NSArray alloc]initWithArray:arr copyItems:NO




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值