//NSDictionary 不可变字典
//字典用来存放key-value(键值对)
//key必须唯一 value必须是对象
//字典是无需集合 所有的操作都通过key实现
//但是创建的时候 value在前 Key在后
//在字典中 value用 object表示
/* NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"lilei", @"name", @"male", @"sex", nil];
NSLog(@"%@", dic1);
//所有key
NSLog(@"%@", dic1.allKeys);
//所有value
NSLog(@"%@", dic1.allValues);
//通过key值访问对应value objectForKey是字典访问
NSLog(@"%@", [dic1 objectForKey:@"name"]);
NSLog(@"-------------");
//字典的遍历
//1.得到key数组
//2.遍历key数组 得到每一个key
//3,通过key得到每一个value
for (NSInteger i = 0; i < dic1.allKeys.count; i++) {
NSString *p = [dic1.allKeys objectAtIndex:i];
NSLog(@"%@ = %@", p, [dic1 objectForKey:p]);
}
*/
/* NSMutableDictionary *mDic = [NSMutableDictionary dictionary];
//添加键值对
//setObject:forKey: 字典方法
//setValue:forKey: KVC方法
//当key值不存在的时候 添加新的key-value对
//当key存在时 找到对应key-value 修改对应的value值
[mDic setObject:@"hanmeimei" forKey:@"name"];
[mDic setObject:@"male" forKey:@"sex"];
[mDic removeObjectForKey:@"sex"];
NSLog(@"%@", mDic);
*/
//练习
//遍历可变字典
//1.创建可变字典
//2.添加key- value对 name sex age hobby
//3.遍历
/* NSMutableDictionary *mStr = [NSMutableDictionary dictionary];
[mStr setObject:@"xiaoming" forKey:@"name"];
[mStr setObject:@"male" forKey:@"sex"];
[mStr setObject:@"20" forKey:@"age"];
[mStr setObject:@"play" forKey:@"hobby"];
for (NSInteger i = 0; i < mStr.allKeys.count; i++) {
NSString *p = [mStr.allKeys objectAtIndex:i];
NSLog(@"%@ %@", p, [mStr objectForKey:p]);
}
*/
//NSSet 不可变集合
//无序 唯一 对象
/* NSSet *set1 = [NSSet setWithObjects:@"a", @"p", @"p", @"l", @"l", @"e", nil];
NSLog(@"%@", set1);
//集合个数
NSLog(@"%ld", set1.count);
//获取某个元素
NSLog(@"%@", [set1 anyObject]);
//是否包含
if ([set1 containsObject:@"p"]) {
NSLog(@"包含");
}
//可变集合
NSMutableSet *set2 = [NSMutableSet set];
//添加
[set2 addObject:@"c"];
[set2 addObject:@"b"];
[set2 addObject:@"a"];
//删除
[set2 removeObject:@"a"];
NSLog(@"%@", set2);
// NSCountedSet 计数集合
NSCountedSet *cSet = [NSCountedSet setWithSet:set2];
[cSet addObject:@"b"];
NSLog(@"%@", cSet);
//计数
NSLog(@"count %ld", [cSet countForObject:@"b"]);
*/
//快读枚举器 for-in
//遍历数组 数组中每个元素
/* NSArray *arr = [NSArray arrayWithObjects:@"1", @"3", @"3", nil];
//获得从集合中得到的每一个元素
for (NSArray *temp in arr) {
NSLog(@"%@", temp);
}
//遍历字典 字典的key 通过key可以访问对应的value
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"lilei", @"name", @"male", @"sex", nil];
for (NSString *key in dic) {
NSLog(@"%@ %@", key, [dic objectForKey:key]);
}
*/
//数组排序
NSArray *arr = [NSArray arrayWithObjects:@"kangKang", @"daShu", @"jiLaoBan", @"haoGe", @"xiaoQiang", @"xiaoMing", nil];
NSLog(@"%@", arr);
//方法选择器
SEL s = @selector(compare:);
NSArray *newArray = [arr sortedArrayUsingSelector:s];
NSLog(@"%@", newArray);
//可变数组的排序
NSMutableArray *marr = [NSMutableArray arrayWithArray:arr];
NSLog(@"%@", marr);
[marr sortUsingSelector:@selector(compare:)];
NSLog(@"%@", marr);