// 遍历总结
// 遍历一般有三种 for循环遍历 枚举器(NSEnumerator)遍历 for ... in 遍历
// 1. for循环遍历字典
NSDictionary *dic =@{@"name": @"edition", @"sex": @"man", @"age": @"36"};
NSArray *dicArr = [dic allKeys];
for (NSInteger i = 0; i < dicArr.count; i++) {
NSString *keyStr = dicAr[i];
NSString *valueStr = [dic ObjectForKey: keStr];
NSLog(@"%@: %@", keyStr, valueStr);
}
// 利用for循环遍历字典 首先把所有的key放到一个数组中 然后遍历这个装了key值得数组 再用字符串接收每个key值 再用 ObjectForKey: 方法获得value
//2. 利用枚举器遍历数组
NSArray *arr = @[@"红色", @"绿色", @"黄色", @"白色"];
NSEnumerator *arrEnu = [arr ObjectEnumerator];
id value = nil;
while (value = [arrEnu nextObject]) {
NSLog(@"%@", value);
}
// 用枚举器遍历数组 首先 要具有一个数组 然后创建枚举器
// 3. 利用枚举器遍历字典 默认输出的是value
NSDictionary *dic = @{@"name": @"john", @"sex": @"man", @"age": @"20"};
NSEnumerator *dicEnu = [NSEnumerator ObjectEnumerator];
id value = nil;
while (value = [dicEnu nextObject]) {
NSLog(@"%@", value);
}
// 枚举器遍历输出key
NSEnumerator *dicEnu = [dic ObjectEnumerator];
id value = nil;
while (value = [dicEnu nextObject]) {
NSLog(@"%@", dic allKeysForObject:value);
}
// 利用 for...in 遍历数组
NSArray *array = @[@"manfan", @"tangtang", @"laoxiang"];
for(NSString *str in array) {
NSLog(@"%@", str);
}
// 利用for... in遍历字典 默认输出的是字典的key值
NSDictionary *dic = @{@"name": @"yueyeu", @"age": @"23", @"sex": @"woman"};
for(NSString *str in dic){
NSLog(@"%@", [dic objectForKey:str]);
}
// 将数组进行升序降序排列 默认升序排列
NSArray *array = @[@"7", @"27", @"57", @"17"];
NSArray *newArray = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@", newArray);
// 排序
NSArray *array = @[@"7", @"27", @"57", @"17"];
NSSortDescriptor *sort = [NSSortDescriptor alloc] initWithKey:nil ascending:YES]; // YES 为升序 NO为降序
NSArray *newArr = [array sortedArrayUsingDescriptors:@[arrSort]];
NSLog(@"%@", newArr);