NSArray的常见用法

1)获取数组常见长度    count获取数组的个数

1 NSArray *arr3 = [NSArray arrayWithObjects:@"one",@"two",@1,@"three",nil];
2 arr3.count

2)根据下标,获取下标对应的对象

[arr3 objectAtIndex];

3)返回元素下标

NSInteger Ioc = [arr3 indexOfObject:@"three"];

NSLog(@"Id",Ioc);

4)数组中包含了某个元素

if([arr3 containObject:@"four"]){

NSLog(@"包含此元素");

}else{

NSLog(@"不包含");

}

用简化的方式 ,来定义和访问数组

1)用简化的方式,定义数组

NSArray *arr = @[@"1",@"2",@"3",@"4"];

格式:@[数组元素]

NSString *str = [arr objectAtIndex:2];

NSlog(@"%@",str);

2)用简化的方式访问数组元素
str = arr[1];
NSLog(@“%@“,str);

NSArray的遍历问题
定义一个数组
NSArray *arr = @[@“one”,@“two”,@“three”,@“four”];
对数组进行遍历
1)普通的方式,通过下标访问
for(int i= 0;i<arr.count;i++){
NSLog(@“-> %@“,arr[i]);
}
2)快速枚举法,for循环的增强形式
for(NSString *str in arr){
NSLog(@“-> %@“,str);
}
    3)使用block方式进行访问
                                                       数组元素              元素下标      是否停止
arr enumerateObjectsUsingBlock:^(id obj,NSUInteger idx,BOOl *stop){
NSLog(@“idx = %ld,obj = %@“,idx,obj);
}];

 
原文地址:https://www.cnblogs.com/quwujin/p/4770623.html