一、创建数组
// 创建一个空的数组 02.NSArray *array = [NSArray array]; 03.// 创建有1个元素的数组 04.array = [NSArray arrayWithObject:@"123"]; 05.// 创建有多个元素的数组 06.array = [NSArray arrayWithObjects:@"a", @"b", @"c", nil nil]; 07.NSArray *array3 = [array arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:@"4", @"5", nil nil]]; 08. 09.NSArray *array4 = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil nil]; 10.NSRange range = NSMakeRange(1, 2); 11.NSArray *array5 = [array4 subarrayWithRange:range];
二、数组的基本用法
int count = [array count];//个数 02.// 判断是否包含了某个元素 03.if ([array containsObject:@"a"]) { 04. NSLog(@"包含了字符串a"); 05.} 06.NSString *last = [array lastObject];最后一个元素 07.NSString *str = [array objectAtIndex:1];根据索引获取数组中的元素 08.int index = [array indexOfObject:@"c"];获取指定元素的索引 09.// 让数组里面的所有对象都调用test方法,123为参数 10.NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, nil nil]; 11.[array makeObjectsPerformSelector:@selector(test2:) withObject:@"123"]; 12.NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil 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];
三、遍历数组
#pragma mark 遍历数组1 02.void arrayFor1() { 03. NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil]; 04. int count = array.count; 05. for (int i = 0; i<count; i++) { 06. id obj = [array objectAtIndex:i]; 07. } 08.} 09. 10.#pragma mark 遍历数组2 快速遍历 11.void arrayFor2() { 12. Student *stu1 = [Student student]; 13. NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil]; 14. int i =0; 15. for (id obj in array) { 16. NSLog(@"%i-%@", i, obj); 17. i++; 18. } 19.} 20. 21.#pragma mark 遍历数组3 22.void arrayFor3() { 23. Student *stu1 = [Student student]; 24. NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil]; 25. [array enumerateObjectsUsingBlock: 26. ^(id obj, NSUInteger idx, BOOLBOOL *stop) { 27. NSLog(@"%i-%@", idx, obj); 28. // 如果索引为1,就停止遍历 29. if (idx == 1) { 30. // 利用指针修改外面BOOL变量的值 31. *stop = YES; 32. } 33. }]; 34.} 35. 36.#pragma mark 遍历数组4 37.void arrayFor4() { 38. Student *stu1 = [Student student]; 39. NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil]; 40. // 获取数组的迭代器 41. // NSEnumerator *enumerator = [array objectEnumerator]; 42. // 反序迭代器(从尾部开始遍历元素) 43. NSEnumerator *enumerator = [array reverseObjectEnumerator]; 44. // allObjects是取出没有被遍历过的对象 45. NSArray *array2 = [enumerator allObjects]; 46. NSLog(@"array2:%@", array2); 47. // 获取下一个需要遍历的元素 48. id obj = nil; 49. while (obj = [enumerator nextObject]) { 50. NSLog(@"obj=%@", obj); 51. } 52.}
四、数组排序
#pragma mark 数组排序1 02.void arraySort1() { 03. NSArray *array = [NSArray arrayWithObjects:@"2", @"3", @"1", @"4", nil nil]; 04. 05. // 返回一个排好序的数组,原来数组的元素顺序不会改变 06. // 指定元素的比较方法:compare: 07. NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)]; 08. NSLog(@"array2:%@", array2); 09.} 10. 11.#pragma mark 数组排序2 12.void arraySort2() { 13. Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"]; 14. Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"]; 15. Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"]; 16. Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"]; 17. NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil]; 18. // 指定排序的比较方法 19. NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)]; 20. NSLog(@"array2:%@", array2); 21.} 22.- (NSComparisonResult)compareStudent:(Student *)stu { 23. // 先按照姓排序 24. NSComparisonResult result = [self.lastname compare:stu.lastname]; 25. // 如果有相同的姓,就比较名字 26. if (result == NSOrderedSame) { 27. result = [self.firstname compare:stu.firstname]; 28. } 29. return result; 30.} 31. 32.#pragma mark 数组排序3 33.void arraySort3() { 34. Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"]; 35. Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"]; 36. Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"]; 37. Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"]; 38. NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil]; 39. 40. // 利用block进行排序 41. NSArray *array2 = [array sortedArrayUsingComparator: 42. ^NSComparisonResult(Student *obj1, Student *obj2) { 43. // 先按照姓排序 44. NSComparisonResult result = [obj1.lastname compare:obj2.lastname]; 45. // 如果有相同的姓,就比较名字 46. if (result == NSOrderedSame) { 47. result = [obj1.firstname compare:obj2.firstname]; 48. } 49. 50. return result; 51. }]; 52. 53. NSLog(@"array2:%@", array2); 54.} 55. 56.#pragma mark 数组排序4-高级排序 57.void arraySort4() { 58. Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li" bookName:@"book1"]; 59. Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang" bookName:@"book2"]; 60. Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li" bookName:@"book2"]; 61. Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao" bookName:@"book1"]; 62. NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil]; 63. 64. // 1.先按照书名进行排序 65. // 这里的key写的是@property的名称 66. NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES]; 67. // 2.再按照姓进行排序 68. NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES]; 69. // 3.再按照名进行排序 70. NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES]; 71. // 按顺序添加排序描述器 72. NSArray *descs = [NSArray arrayWithObjects:bookNameDesc, lastnameDesc, firstnameDesc, nil nil]; 73. 74. NSArray *array2 = [array sortedArrayUsingDescriptors:descs]; 75. 76. NSLog(@"array2:%@", array2); 77.}