字典、集合、数组排序、字典、集合的快速遍历
#import <Foundation/Foundation.h>
//定义一个排序的条件的函数
NSInteger sort1 (id s1, id s2, void *p){
// NSInteger a = [s1 integerValue];
// NSInteger b = [s2 integerValue];
//
// if (a>b) {
// return 2;
// } else if(a<b){
// return 0;
// } else {
// return 0;
// }
NSString *str1 = (NSString *)s1;
NSString *str2 = (NSString *)s2;
// str1 > str2
if ([str1 compare:str2] == 1) {
return 1;
// str1 < str2
}
else if ([str1 compare:str2] == -1) {
return -1;
}
return 0;
}
int main(int argc, const char * argv[]) {
/*
NSArray *array = [NSArray arrayWithObjects:@"12",@"22",@"42",@"32", nil];
// 使用系统排序方法,传入的参数是comapre:方法的函数指针;排序的判断条件使用的是compare:的返回值
// 系统提供的这个排序方法 让数组升序排列;
NSArray *sortArray = [array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"sortArray = %@",sortArray);
// 使用自定义的排序条件函数,如果返回值是正数,执行交换;
NSArray *sort = [array sortedArrayUsingFunction:sort1 context:nil];
NSLog(@"sort = %@",sort);
// 对可变数组进行排序,操作的还是原内存;用系统默认的排序方法,是升序排序;如果想实现倒序排序可以自己定义排序函数,返回值为正数就进行排序
NSMutableArray *mutableArray = [NSMutableArray arrayWithObjects:@"zhangsan",@"lisi",@"amao",@"wangwu", nil];
// [mutableArray sortUsingSelector:@selector(compare:)];
[mutableArray sortUsingFunction:sort1 context:nil];
NSLog(@"mutableArray = %@",mutableArray);
*/
// 通过字典声明的自定义的初始化方法来创建对象,字典里面每一个元素都是成对出现
NSDictionary *zhangsan = [[NSDictionary alloc]initWithObjectsAndKeys:@"zhangsan",@"name",@"男",@"sex", nil ];
NSLog(@"zhangsan = %@",zhangsan);
// 使用便利构造器来创建字典对象
NSDictionary *lisi = [NSDictionary dictionaryWithObjectsAndKeys:@"lisi",@"name",@"女",@"sex", nil];
NSLog(@"lisi = %@",lisi);
//
NSDictionary *wangwu = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"wangwu",@"男", nil] forKeys:[NSArray arrayWithObjects:@"name",@"sex", nil]];
NSLog(@"wangwu = %@",wangwu);
// 获取字典对象的所有的key值的方法是allkeys;
NSLog(@"allkeys = %@",[wangwu allKeys]);
// 获取字典对象的所有的value值的方法是allValues
NSLog(@"allvalues = %@", [wangwu allValues]);
// 字典取值 通过key 获取相应的value
NSLog(@"name = %@",[wangwu objectForKey:@"name"]);
// 创建一个可变字典对象
NSMutableDictionary *mutableDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"name",[NSNumber numberWithInt:20],@"age", nil];
// 如果字典中没有设置的key值, 就是向字典中添加元素;如果key已经存在就是修改value值
[mutableDic setObject:@"beijing" forKey:@"address"];
NSLog(@"mutableDic = %@",mutableDic);
[mutableDic setObject:@"wuhan" forKey:@"address"];
NSLog(@"mutableDic = %@",mutableDic);
// 用setObject: 这个方法的时候,object不能给nil
// [mutableDic setObject:niu forKey:@"height"];
// 用setValue:对字典进行设置,如果value的值是nil 而且key在字典中存在,就是删除
[mutableDic setValue:@"170" forKey:@"height"];
NSLog(@"mutableDic = %@",mutableDic);
// 使用remove方法来删除字典中的key-value 对
[mutableDic removeObjectForKey:@"height"];
NSLog(@"mutableDic = %@", mutableDic);
// for (int = 0; i < [[mutableDic allKeys] count]; i++) {
// id key = [[mutableDic allKeys] objectAtIndex:i];
// NSLog(@"%@ : %@",key, [mutableDic objectForKey:key]);
// }
for (id key in [mutableDic allKeys]) {
NSLog(@"%@ : %@",key, [mutableDic objectForKey:key]);
}
// 用字典表示一个区
NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"海淀",@"area",@"20w",@"personCount", nil];
NSDictionary *dic2 = [NSDictionary dictionaryWithObjectsAndKeys:@"朝阳",@"area",@"30w",@"personCount", nil];
NSArray *areaArray = [NSArray arrayWithObjects:dic1,dic2, nil];
NSDictionary *cityDic = [NSDictionary dictionaryWithObjectsAndKeys:@"beijing",@"cityName", areaArray,@"area", nil];
NSLog(@"cityDic = %@",cityDic);
// 集合中不能放重复的对象
NSNumber *num1 = [NSNumber numberWithInt:5];
NSNumber *num2 = [NSNumber numberWithInt:2];
NSNumber *num3 = [NSNumber numberWithInt:31];
NSSet *set = [NSSet setWithObjects:num1,num2,num2,num3, nil];
NSLog(@"set = %@",set);
NSLog(@"set.count = %lu",set.count);
// member:判断对象是不是集合的成员 如果不在集合里面就返回空,如果在就返回该对象
NSLog(@"object = %@",[set member:num2]);
// 任意取一个对象并且返回
NSLog(@"anyOabject = %@",[set anyObject]);
// 判断集合中是否包含这个元素,
NSLog(@"containsObject = %d", [set containsObject:num2]);
// 创建一个可变的集合对象,用已经存在的集合对象来创建
NSMutableSet *mutableSet = [NSMutableSet setWithSet:set];
NSNumber *num4 = [NSNumber numberWithInt:4];
NSSet *set1 = [NSSet setWithObjects:num3,num4,nil];
// 取两个set的交集并放到可变数组中,不相交的元素都不要
[mutableSet intersectSet:set1] ;
NSLog(@"mutableSet = %@", mutableSet);
NSCountedSet *countSet = [NSCountedSet setWithObjects:num4,num3,num2,num2,num3,num1, nil];
// 计算集合中元素的重复的次数
NSLog(@"countSet = %lu",[countSet countForObject:num1]);
// 用for in 进行快速枚举遍历 来 查找集合中的每一个元素
for (id s in countSet) {
NSLog(@"s= %@",s);
}
return 0;
}