您的位置:首页 > 移动开发 > IOS开发

《慕客网:IOS基础入门之Foundation框架初体验》学习笔记 <三> NSArray

2016-03-11 00:00 621 查看
1 int main(int argc, const char * argv[]) {  2  @autoreleasepool {  3         //OC数组 可以存储不同类型的对象 只能存储对象,基本数据类型是不能存储的  4         //OC数组 存储的是对象的指针
5         NSArray *array = [[NSArray alloc] initWithObjects:@"1",@"2",@"3",@"4",@"5", nil];  6         //数组的长度
7         int count = (int)array.count;  8         NSLog(@"count :%d",count);  9         //判断是否存在某个对象
10         BOOL isHave = [array containsObject:@"3"]; 11         if (isHave) { 12             NSLog(@"存在"); 13         }else { 14             NSLog(@"不存在"); 15  } 16         //输出最后一个对象
17         NSString *str = [array lastObject]; 18         NSLog(@"str = %@",str); 19         //输出第一个对象
20         NSString *str2 = [array firstObject]; 21         NSLog(@"str2 = %@",str2); 22         //取出数组中指定下标的对象
23         str = [array objectAtIndex:3]; 24         NSLog(@"str = %@",str); 25         //也可以通过具体元素来返回所在的下标位置
26         int index = (int)[array indexOfObject:@"1"]; 27         NSLog(@"index = %d",index);//如果没有指定的元素,返回的是-1 28
29         //数组的遍历(1.基本的for循环通过下标逐一取出查看2.for in 快速枚举 3.枚举器(或者叫迭代器))
30         People *p = [[People alloc] init]; 31         p.name = @"张三"; 32         NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"b",p,@"d",@"e", nil]; 33         //1.
34         for (int i = 0; i<array.count; i++) { 35             NSString *str1 = [array2 objectAtIndex:i]; 36             NSLog(@"%@",str1); 37  } 38         //2.如果需要使用快速枚举,我们需要让数组中元素的类型保持一致
39         for (NSString *st in array) { 40             NSLog(@"duang : %@",st); 41  } 42         //3.略
43  } 44     return 0; 45 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: