数组的一些基本方法
int count = [array count];//个数 // 判断是否包含了某个元素 if ([array containsObject:@"a"]) { NSLog(@"包含了字符串a"); } NSString *last = [array lastObject];最后一个元素 NSString *str = [array objectAtIndex:1];根据索引获取数组中的元素 int index = [array indexOfObject:@"c"];获取指定元素的索引 // 让数组里面的所有对象都调用test方法,123为参数 NSArray *array = [NSArray arrayWithObjects:stu1, stu2, stu3, nil nil]; [array makeObjectsPerformSelector:@selector(test2:) withObject:@"123"]; NSArray *array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil nil]; // 1-2-3-4 // 利用分隔符-拼接所有的数组元素 NSString *str = [array componentsJoinedByString:@"-"]; // 将一个数组写入文件(生成的是一个xml文件) NSString *path = @"/Users/apple/Desktop/array.xml"; [array writeToFile:path atomically:YES]; path = @"/Users/apple/Desktop/array.txt"; // 从文件中读取数组内容(文件有严格的格式要求) NSArray *array2 = [NSArray arrayWithContentsOfFile:path];
遍历数组
#pragma mark 遍历数组1 void arrayFor1() { NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil]; int count = array.count; for (int i = 0; i<count; i++) { id obj = [array objectAtIndex:i]; } } #pragma mark 遍历数组2 快速遍历 void arrayFor2() { Student *stu1 = [Student student]; NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil]; int i =0; for (id obj in array) { NSLog(@"%i-%@", i, obj); i++; } } #pragma mark 遍历数组3 void arrayFor3() { Student *stu1 = [Student student]; NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil]; [array enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOLBOOL *stop) { NSLog(@"%i-%@", idx, obj); // 如果索引为1,就停止遍历 if (idx == 1) { // 利用指针修改外面BOOL变量的值 *stop = YES; } }]; } #pragma mark 遍历数组4 void arrayFor4() { Student *stu1 = [Student student]; NSArray *array = [NSArray arrayWithObjects:stu1, @"1", @"2", @"3", nil nil]; // 获取数组的迭代器 // NSEnumerator *enumerator = [array objectEnumerator]; // 反序迭代器(从尾部开始遍历元素) NSEnumerator *enumerator = [array reverseObjectEnumerator]; // allObjects是取出没有被遍历过的对象 NSArray *array2 = [enumerator allObjects]; NSLog(@"array2:%@", array2); // 获取下一个需要遍历的元素 id obj = nil; while (obj = [enumerator nextObject]) { NSLog(@"obj=%@", obj); } }
数组排序
#pragma mark 数组排序1 void arraySort1() { NSArray *array = [NSArray arrayWithObjects:@"2", @"3", @"1", @"4", nil nil]; // 返回一个排好序的数组,原来数组的元素顺序不会改变 // 指定元素的比较方法:compare: NSArray *array2 = [array sortedArrayUsingSelector:@selector(compare:)]; NSLog(@"array2:%@", array2); } #pragma mark 数组排序2 void arraySort2() { Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"]; Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"]; Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"]; Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"]; NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil]; // 指定排序的比较方法 NSArray *array2 = [array sortedArrayUsingSelector:@selector(compareStudent:)]; NSLog(@"array2:%@", array2); } - (NSComparisonResult)compareStudent:(Student *)stu { // 先按照姓排序 NSComparisonResult result = [self.lastname compare:stu.lastname]; // 如果有相同的姓,就比较名字 if (result == NSOrderedSame) { result = [self.firstname compare:stu.firstname]; } return result; } #pragma mark 数组排序3 void arraySort3() { Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li"]; Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang"]; Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li"]; Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao"]; NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil]; // 利用block进行排序 NSArray *array2 = [array sortedArrayUsingComparator: ^NSComparisonResult(Student *obj1, Student *obj2) { // 先按照姓排序 NSComparisonResult result = [obj1.lastname compare:obj2.lastname]; // 如果有相同的姓,就比较名字 if (result == NSOrderedSame) { result = [obj1.firstname compare:obj2.firstname]; } return result; }]; NSLog(@"array2:%@", array2); } #pragma mark 数组排序4-高级排序 void arraySort4() { Student *stu1 = [Student studentWithFirstname:@"MingJie" lastname:@"Li" bookName:@"book1"]; Student *stu2 = [Student studentWithFirstname:@"LongHu" lastname:@"Huang" bookName:@"book2"]; Student *stu3 = [Student studentWithFirstname:@"LianJie" lastname:@"Li" bookName:@"book2"]; Student *stu4 = [Student studentWithFirstname:@"Jian" lastname:@"Xiao" bookName:@"book1"]; NSArray *array = [NSArray arrayWithObjects:stu1,stu2,stu3, stu4, nil nil]; // 1.先按照书名进行排序 // 这里的key写的是@property的名称 NSSortDescriptor *bookNameDesc = [NSSortDescriptor sortDescriptorWithKey:@"book.name" ascending:YES]; // 2.再按照姓进行排序 NSSortDescriptor *lastnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"lastname" ascending:YES]; // 3.再按照名进行排序 NSSortDescriptor *firstnameDesc = [NSSortDescriptor sortDescriptorWithKey:@"firstname" ascending:YES]; // 按顺序添加排序描述器 NSArray *descs = [NSArray arrayWithObjects:bookNameDesc, lastnameDesc, firstnameDesc, nil nil]; NSArray *array2 = [array sortedArrayUsingDescriptors:descs]; NSLog(@"array2:%@", array2); }