标签:
1 NSArray *cityArray = [NSArray arrayWithObjects:@"中国北京",@"中国郑州",@"中国洛阳",@"中国杭州",@"中国香港",@"中国台湾", nil]; 2 NSDictionary *personInforDic = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",@"nan",@"sex",[NSNumber numberWithInt:28],@"age",nil]; 3 NSSet *stringSet = [NSSet setWithObjects:@"123",@"456",@"789",@"0X0",nil]; 4 // 5 /* 6 for循环遍历 7 */ 8 //遍历数组 9 for(int i = 0;i < cityArray.count;i++){ 10 NSString *string = [cityArray objectAtIndex:i]; 11 NSLog(@"cityArray[%d] = %@",i,string); 12 } 13 //遍历字典 14 NSArray *keys = [personInforDic allKeys]; 15 for (int i = 0; i < personInforDic.count; i++) { 16 NSString *key = [keys objectAtIndex:i ]; 17 NSString *value = [personInforDic valueForKey:key]; 18 NSLog(@"%@ = %@",key,value); 19 } 20 //遍历集合 21 NSArray *setObject = [stringSet allObjects]; 22 for (int i = 0; i < stringSet.count; i++) { 23 NSLog(@"%@",[setObject objectAtIndex:i]); 24 } 25 26 /* 27 NSEnumerator 28 */ 29 //NSEnumerator 数组----正序遍历 30 NSEnumerator *arrEnu = [cityArray objectEnumerator]; 31 32 id value = nil; 33 while (value = [arrEnu nextObject]) { 34 NSLog(@"%@",value); 35 } 36 NSLog(@"------------------"); 37 //NSEnumerator 数组----倒序遍历 38 NSEnumerator *arrayEnu = [cityArray reverseObjectEnumerator]; 39 id value1 = nil; 40 while (value1 = [arrayEnu nextObject]) { 41 NSLog(@"%@",value1); 42 } 43 44 //NSEnumerator 字典 45 NSEnumerator *arrEnu1 = [personInforDic objectEnumerator]; 46 47 id value2 = nil; 48 while (value2 = [arrEnu1 nextObject]) { 49 NSLog(@"%@",value2); 50 } 51 //NSEnumerator 集合 52 NSEnumerator *setEnu = [stringSet objectEnumerator]; 53 id value3 = nil; 54 while (value3 = [setEnu nextObject]) { 55 NSLog(@"%@",value3); 56 } 57 /* 58 for----in 遍历 59 */ 60 //for----in 遍历数组 61 for (NSString *cityName in cityArray) { 62 NSLog(@"cityName = %@",cityName); 63 } 64 //for----in 遍历字典 65 for (NSString *key in personInforDic) { 66 NSLog(@"%@ = %@",key,[personInforDic objectForKey:key]); 67 } 68 //for----in 遍历集合 69 for (NSString *ss in setEnu) { 70 NSLog(@"%@",ss); 71 } 72 // 73 #pragma mark------数组排序--------- 74 75 76 //创建一个可变数组 77 // NSMutableArray *sortArray = [NSMutableArray arrayWithObjects:@"aa",@"bb",@"cc",@"ee",@"dd", nil]; 78 // for (int i = 0; i < sortArray .count - 1; i++) { 79 // for (int k = 0; k < sortArray.count - i - 1; k++) { 80 // if ([sortArray[k] compare:sortArray[k+1]] > 0) { 81 // [sortArray exchangeObjectAtIndex:k withObjectAtIndex:k+1]; 82 // } 83 // } 84 // } 85 // NSLog(@"%@",sortArray); 86 //sortedArrayUsingSelector 是有返回值的,返回值是排序之后的数组 87 // NSArray *sortArray1 = [sortArray sortedArrayUsingSelector:@selector(compare:)]; 88 // NSLog(@"%@",sortArray1); 89 // //sortUsingSelector 这是一个没有返回值的,改变的就是调用这个方法的数组的本身 90 // [sortArray sortUsingSelector:@selector(compare:)]; 91 // NSLog(@"%@",sortArray); 92 // //降序排序 93 // NSMutableArray *array = [NSMutableArray array]; 94 // //上面的语句相当于 NSMutableArray *array = [[NSMutableArray alloc] init]; 95 // for (NSInteger i = sortArray.count - 1 ; i >= 0; i--) { 96 // [array addObject:sortArray[i]]; 97 // } 98 // NSLog(@"%@",array); 99 /* 100 数组排序 ------数组中存放自定义类的对象 101 */ 102 Person *per1 = [Person personWithName:@"zhang1" Age:18 Sex:@"nan"]; 103 Person *per2 = [Person personWithName:@"zhang2" Age:19 Sex:@"nan"]; 104 Person *per3 = [Person personWithName:@"zhang3" Age:20 Sex:@"nan"]; 105 Person *per4 = [Person personWithName:@"zhang4" Age:21 Sex:@"nan"]; 106 Person *per5 = [Person personWithName:@"zhang5" Age:22 Sex:@"nan"]; 107 108 //根据多个person对象,生成数组 109 NSArray *personArray = [NSArray arrayWithObjects:per1,per2,per3,per4,per5, nil]; 110 // //不可变数组排序 111 // //生成排序描述符 112 // NSSortDescriptor *sortName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES]; 113 // //通过排序描述符对数组进行排序,生成新的数组 114 // NSArray *sortedName = [personArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortName]]; 115 // NSLog(@"%@",sortedName); 116 // //按照年龄降序排序 117 // NSSortDescriptor *sortAge = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:NO]; 118 // NSArray *sortedAge = [personArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortAge]]; 119 // NSLog(@"%@",sortedAge); 120 //可变数组 121 NSMutableArray *marray = [personArray mutableCopy]; 122 123 //生成排序描述符 --------按人的姓名降序 124 NSSortDescriptor *sortWithName = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:NO]; 125 126 //通过排序描述符对数组进行排序 127 [marray sortUsingDescriptors:[NSArray arrayWithObject: sortWithName]]; 128 NSLog(@"%@",marray);
标签:
原文地址:http://www.cnblogs.com/DevinSMR/p/5138409.html