A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© qq541955920 中级黑马   /  2014-8-20 13:07  /  1007 人查看  /  0 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

1创建数组

  1. <div> // 创建一个空的数组</div><div> NSArray *array = [NSArray array];    </div><div> // 创建有1个元素的数组</div><div> array = [NSArray arrayWithObject:@"123"];</div><div> // 创建有多个元素的数组</div><div> array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil];</div><div> NSArray *array3 = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"4", @"5", nil]];</div><div>
  2. </div><div> NSArray *array4 = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];</div><div> NSRange range = NSMakeRange(1, 2);</div><div> NSArray *array5 = [array4 subarrayWithRange:range];</div>
复制代码
2数组的一些基本方法
  1. int count = [array count];//个数
  2. // 判断是否包含了某个元素
  3. if ([array containsObject:@"a"]) {
  4.     NSLog(@"包含了字符串a");
  5. }
  6. NSString *last = [array lastObject];最后一个元素
  7. NSString *str = [array objectAtIndex:1];根据索引获取数组中的元素
  8. int index = [array indexOfObject:@"c"];获取指定元素的索引
  9. // 让数组里面的所有对象都调用test方法,123为参数
  10. NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, nil];
  11. [array makeObjectsPerformSelector:@selector(test2:) withObject:@"123"];
  12. NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
  13. // 1-2-3-4
  14. // 利用分隔符-拼接所有的数组元素
  15. NSString *str = [array componentsJoinedByString:@"-"];
  16. // 将一个数组写入文件(生成的是一个xml文件)
  17. NSString *path = @"/Users/apple/Desktop/array.xml";
  18. [array writeToFile:path atomically:YES];
  19. path = @"/Users/apple/Desktop/array.txt";
  20. // 从文件中读取数组内容(文件有严格的格式要求)
  21. NSArray *array2 = [NSArray arrayWithContentsOfFile:path];
复制代码
3遍历数组

  1. #pragma mark 遍历数组1
  2. void arrayFor1() {
  3.     NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
  4.     int count = array.count;
  5.     for (int i = 0; i<count; i++) {
  6.         id obj = [array objectAtIndex:i];
  7.     }
  8. }

  9. #pragma mark 遍历数组2 快速遍历
  10. void arrayFor2() {
  11.     Student *stu1 = [Student student];
  12.     NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
  13.     int i =0;
  14.     for (id obj in array) {
  15.         NSLog(@"%i-%@", i, obj);
  16.         i++;
  17.     }
  18. }

  19. #pragma mark 遍历数组3
  20. void arrayFor3() {
  21.     Student *stu1 = [Student student];
  22.     NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
  23.     [array enumerateObjectsUsingBlock:
  24.      ^(id obj, NSUInteger idx, BOOL *stop) {
  25.         NSLog(@"%i-%@", idx, obj);
  26.          // 如果索引为1,就停止遍历
  27.          if (idx == 1) {
  28.              // 利用指针修改外面BOOL变量的值
  29.              *stop = YES;
  30.          }
  31.     }];
  32. }

  33. #pragma mark 遍历数组4
  34. void arrayFor4() {
  35.     Student *stu1 = [Student student];
  36.     NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil];
  37.     // 获取数组的迭代器
  38.     // NSEnumerator *enumerator = [array objectEnumerator];
  39.     // 反序迭代器(从尾部开始遍历元素)
  40.     NSEnumerator *enumerator = [array reverseObjectEnumerator];
  41.     // allObjects是取出没有被遍历过的对象
  42.     NSArray *array2 = [enumerator allObjects];
  43.     NSLog(@"array2:%@", array2);
  44.     // 获取下一个需要遍历的元素
  45.     id obj = nil;
  46.     while (obj = [enumerator nextObject]) {
  47.         NSLog(@"obj=%@", obj);
  48.     }
  49. }
复制代码
4数组排序
  1. #pragma mark 数组排序1
  2. void arraySort1() {
  3.     NSArray *array = [NSArray arrayWithObjects:@"2", @"3", @"1", @"4", nil];
  4.    
  5.     // 返回一个排好序的数组,原来数组的元素顺序不会改变
  6.     // 指定元素的比较方法:compare:
  7.     NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)];
  8.     NSLog(@"array2:%@", array2);
  9. }

  10. #pragma mark 数组排序2
  11. void arraySort2() {
  12.     Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];
  13.     Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];
  14.     Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];
  15.     Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];
  16.     NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];
  17.     // 指定排序的比较方法
  18.     NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)];
  19.     NSLog(@"array2:%@", array2);
  20. }
  21. - (NSComparisonResult)compareStudent:(Student *)stu {
  22.     // 先按照姓排序
  23.     NSComparisonResult result = [self.lastname compare:stu.lastname];
  24.     // 如果有相同的姓,就比较名字
  25.     if (result == NSOrderedSame) {
  26.         result = [self.firstname compare:stu.firstname];
  27.     }
  28.     return result;
  29. }

  30. #pragma mark 数组排序3
  31. void arraySort3() {
  32.     Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"];
  33.     Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"];
  34.     Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"];
  35.     Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"];
  36.     NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];
  37.    
  38.     // 利用block进行排序
  39.     NSArray *array2 = [array sortedArrayUsingComparator:
  40.      ^NSComparisonResult(Student *obj1, Student *obj2) {
  41.          // 先按照姓排序
  42.          NSComparisonResult result = [obj1.lastname compare:obj2.lastname];
  43.          // 如果有相同的姓,就比较名字
  44.          if (result == NSOrderedSame) {
  45.              result = [obj1.firstname compare:obj2.firstname];
  46.          }
  47.          
  48.          return result;
  49.     }];
  50.    
  51.     NSLog(@"array2:%@", array2);
  52. }

  53. #pragma mark 数组排序4-高级排序
  54. void arraySort4() {
  55.     Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li" bookName:@"book1"];
  56.     Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang" bookName:@"book2"];
  57.     Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li" bookName:@"book2"];
  58.     Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao" bookName:@"book1"];
  59.     NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil];
  60.    
  61.     // 1.先按照书名进行排序
  62.     // 这里的key写的是@property的名称
  63.     NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES];
  64.     // 2.再按照姓进行排序
  65.     NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES];
  66.     // 3.再按照名进行排序
  67.     NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES];
  68.     // 按顺序添加排序描述器
  69.     NSArray *descs = [NSArray arrayWithObjects:bookNameDesc, lastnameDesc, firstnameDesc, nil];
  70.    
  71.     NSArray *array2 = [array sortedArrayUsingDescriptors:descs];
  72.    
  73.     NSLog(@"array2:%@", array2);
  74. }
复制代码




0 个回复

您需要登录后才可以回帖 登录 | 加入黑马