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