一 、遍历
For 循环遍历
#数组遍历
NSArray *array = @[@"zhang",@"wang",@"li"];
for (int i = 0; i < array.count;i++){
NSLog(@"------%@ \n",array[i]);
}
#字典遍历
NSDictionary *dic = @{@"name":@"tom",
@"age":@"18",
@"sex":@"male"
};
// 通过 key
NSArray *keys = dic.allKeys;
for(int i = 0; i< keys.count; i++){
NSString *key = keys[i];
NSString *str = [dic objectForKey:key];
NSLog(@"%@",str);
}
//集合遍历
NSSet *set = [NSSet setWithObjects:@"zhang",@"wang",@"li", nil];
// 取出所有对象,变为数组
NSArray *temp = set.allObjects;
for (int i = 0; i < temp.count; i++) {
NSLog(@"%@",temp[i]);
}
NSEnumerator 枚举器遍历
数组,字典,集合都有一个枚举器方法,返回的是枚举器对象
#数组枚举
// 定义一个数组
NSArray *array = @[@"wang",@"li",@"zhang"];
// 正向枚举
NSEnumerator *enumerator = [array objectEnumerator];
id objecet;
while (objecet = [enumerator nextObject]) {
NSLog(@"正向枚举%@",objecet);
}
// 反向枚举
NSEnumerator *reverse = [array reverseObjectEnumerator];
id result2; //
while (result2 = [reverse nextObject]) {
NSLog(@"反向枚举%@", result2);
}
#字典枚举
NSDictionary *dic = @{@"name":@"tom",
@"age":@"20",
@"sex":@"male"};
NSEnumerator *enumDic = [dic objectEnumerator];
id objecetInDic;
while (objecetInDic = [enumDic nextObject]) {
NSLog(@"枚举器---%@\n",objecetInDic);
}
# 集合枚举
NSSet *set = [NSSet setWithObjects:@"zhang",@"wang",@"li", nil];
NSEnumerator *enumSet = [set objectEnumerator];
id objectInSet;
while (objectInSet = [enumSet nextObject]) {
NSLog(@"集合---%@\n",objectInSet);
}
for ..in 遍历
1.对于数组,forin 枚举的是对象;
2,对于字典,forin 枚举的是key;
3,对于集合,forin 枚举的是对象;
二、数组排序
方法 一:NSSortDescriptor
NSArray *arr = @[@"zhang",@"wang",@"lili"];
// 创建排序描述对象
// 降序(ascending :NO) 升序(ascending :YES)
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];
// 调用数组的排序方法
NSArray *newArr = [arr sortedArrayUsingDescriptors:@[descriptor]];
// 遍历输出
for (NSString *string in newArr) {
NSLog(@"-----数组%@",string);
}
- 建一个 Person 类
Person.h
@interface Person : NSObject
@property (nonatomic, retain) NSString *name;
@property (nonatomic, assign) NSInteger age;
@property (nonatomic, retain) NSString *sex;
-(instancetype)initWithName:(NSString *)name age:(NSInteger)age sex:(NSString *)sex;
/** 声明一个按照姓名升序排序的方法. */
- (NSComparisonResult)compareWithNameUP:(Person *)anotherPerson;
/** 声明一个按照姓名 #降序# 排序的方法. */
- (NSComparisonResult)compareWithNameDown:(Person *)anotherPerson;
/**根据年龄进行 #升序# 排序方法大 */
- (NSComparisonResult)compareWithAgeUP:(Person *)anotherPerson;
Person.m
#import "Person.h"
@implementation Person
/**重新初始化方法 */
-(instancetype)initWithName:(NSString *)name age:(NSInteger)age sex:(NSString *)sex;{
self = [super init];
if (self) {
self.name = name;
self.age = age;
self.sex = sex;
}
return self;
}
/** 声明一个按照姓名升序排序的方法. */
- (NSComparisonResult)compareWithNameUP:(Person *)anotherPerson{
return [self.name compare:anotherPerson.name];
}
/** 声明一个按照姓名 #降序# 排序的方法. */
- (NSComparisonResult)compareWithNameDown:(Person *)anotherPerson{
return [self.name compare:anotherPerson.name] * -1; // - 1 将升序变为降序
}
/**根据年龄进行 #升序# 排序方法大 */
- (NSComparisonResult)compareWithAgeUP:(Person *)anotherPerson{
if (self.age >anotherPerson.age){
return NSOrderedDescending;
}else if (self.age < anotherPerson.age){
return NSOrderedAscending;
}else{
return NSOrderedSame;
}
}
Person 数组对象排序
Person *per1 = [[Person alloc] initWithName:@"zhangsan" age:20 sex:@"male"];
Person *per2 = [[Person alloc] initWithName:@"xiaoming" age:18 sex:@"male"];
Person *per3 = [[Person alloc] initWithName:@"xiaoxue" age:22 sex:@"female"];
Person *per4 = [[Person alloc] initWithName:@"xiaoli" age:17 sex:@"female"];
Person *per5 = [[Person alloc] initWithName:@"tom" age:18 sex:@"female"];
NSArray *arr2 = @[per1, per2, per3, per4, per5];// 数组类型
//需求:按照年龄升序排序,如果年龄相同,再按照姓名升序排序
NSSortDescriptor *name = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES ];
NSSortDescriptor *age = [NSSortDescriptor sortDescriptorWithKey:@"age" ascending:YES];
//排序
NSArray *newArr1 = [arr2 sortedArrayUsingDescriptors:@[age, name]];
for (Person *per in newArr1) {
NSLog(@"%@ , %ld",per.name, per.age);
}
- 打印结果
2016-06-16 09:54:21.671 遍历和数组排序[8222:657731] 名字xiaoli , 年龄17
2016-06-16 09:54:21.671 遍历和数组排序[8222:657731] 名字tom , 年龄18
2016-06-16 09:54:21.671 集合遍历和数组排序[8222:657731] 名字xiaoming , 年龄18
2016-06-16 09:54:21.671 集合遍历和数组排序[8222:657731] 名字zhangsan , 年龄20
2016-06-16 09:54:21.671 集合遍历和数组排序[8222:657731] 名字xiaoxue , 年龄22
方法 二 : sortedArrayUsingSeletector 方法
注意: 字符串比较的是 ASCII 码值大小
例如:
NSString *a = @"aaaA";//'A'的ASCII码值是65
NSString *b = @"aaaa";//'a'的ASCII码值是97
// 数组中都是字符串对象
NSArray *arr = @[@"zhangsan",@"lisi",@"wang"];
// @param SEL 方法选择器,@selector (方法名) ,方法的返回值必须是 NSComparisonResult (比较)
// 注意,这个方法是数组中元素的方法,所以需要数组的排序 ;
NSArray *NewArr = [arr sortedArrayUsingSelector:@selector(compare:)];
for (NSString *string in NewArr) {
NSLog(@"%@",string);
}
方法 三 :自定义类数组排序,例如上面 Person 类的数组中都是 peson 对象,按照 name 升序排序
Person *per1 = [[Person alloc] initWithName:@"zhangsan" age:20 sex:@"male"];
Person *per2 = [[Person alloc] initWithName:@"xiaoming" age:18 sex:@"male"];
Person *per3 = [[Person alloc] initWithName:@"xiaoxue" age:22 sex:@"female"];
Person *per4 = [[Person alloc] initWithName:@"xiaoli" age:17 sex:@"female"];
Person *per5 = [[Person alloc] initWithName:@"tom" age:18 sex:@"female"];
NSArray *arr2 = @[per1, per2, per3, per4, per5];
//姓名 升序
NSArray *NewArr2 = [arr2 sortedArrayUsingSelector:@selector(compareWithNameUP:)];
for (Person *per in NewArr2) {
NSLog(@"%@",per.name);
}
// 姓名 降序
NSArray *NewArr3 = [arr2 sortedArrayUsingSelector:@selector(compareWithNameDown:)];
for (Person *per in NewArr3) {
NSLog(@"%@",per.name);
}
// 根据年龄 进行升序
NSArray *ageUP = [arr2 sortedArrayUsingSelector:@selector(compareWithAgeUP:)];
for (Person *per in ageUP) {
NSLog(@"%ld",per.age);
}
方法 四: sortedArrayUsingComparator Block块儿
NSArray *arr = @[@"wang",@"zhang",@"lisi"];
NSArray *newarr = [arr sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];//升序
}];
for (NSString *string in newarr) {
NSLog(@"代码块儿:%@",string);
}
/// 构造类
Person *per1 = [[Person alloc] initWithName:@"zhangsan" age:20 sex:@"male"];
Person *per2 = [[Person alloc] initWithName:@"xiaoming" age:18 sex:@"male"];
Person *per3 = [[Person alloc] initWithName:@"xiaoxue" age:22 sex:@"female"];
Person *per4 = [[Person alloc] initWithName:@"xiaoli" age:17 sex:@"female"];
Person *per5 = [[Person alloc] initWithName:@"tom" age:18 sex:@"female"];
NSArray *arr2 = @[per1, per2, per3, per4, per5];
// 按照 person 的name 升序排序
NSArray *newarr2 = [arr2 sortedArrayUsingComparator:^NSComparisonResult(Person* _Nonnull obj1, Person* _Nonnull obj2) {
return [obj1.name compare:obj2.name ];
}];
for (Person *per in newarr2) {
NSLog(@"%@", per.name);
}
// 按照person的 age 降序排序
NSArray *ageDown = [arr2 sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
Person *per1 = obj1;
Person *per2 = obj2;
if (per1.age > per2.age) {
return NSOrderedAscending;// 升序
}else if (per1.age < per2.age){
//需求降序,前一个小于后一个,两个数掉换一下 Descending 改变if的排序 降序 + dssending (降序) 变为升序 ; 升序(<) + dssending (降序) 变为降序
return NSOrderedDescending;// 降序
}else{
return NSOrderedSame;
}
}];
for (Person *per in ageDown) {
NSLog(@"%ld", per.age);
}
方法 五: 可变数组排序
NSMutableArray *mArr = @[@"wang",@"zhang",@"lisi"].mutableCopy;
// 升序排序,数组本身进行了交换, 直接看方法是否有返回值,有返回值为不可变数组
[mArr sortUsingSelector:@selector(compare:)];