//字典, 用来存储具有一一对应关系的数据,例如,name:XiaoLong price:30
//本质上也是一个大容器,用来存储数据
//value必须为对象, key一般为NSString
#import
int main(int argc, const char * argv[]) {
@autoreleasepool {
//1.使用初始化方法创建字典对象(最常用的创建字典对象的方法) value在前,key在后,在创建的时候不要有两个及两个以上的 相同的 key,一个key对应一个value,如果具有多个相同的value是不影响的
NSDictionary *dic = [[NSDictionary alloc]initWithObjectsAndKeys:@"XiaoLong",@"name",@"m",@"sex",@"18",@"age",nil];
NSLog(@"dic = %@",dic);
//第二种创建字典对象的方式
//对于这种方法,会有数组参数,前面是传入values,后面是传入keys,而且values与keys一一对应的
NSDictionary *dic2 = [[NSDictionary alloc] initWithObjects:[NSArrayarrayWithObjects:@"DouDou",@"Frank",@"AnLong", nil] forKeys:[NSArrayarrayWithObjects:@"D",@"F",@"A", nil]];
NSLog(@"%@",dic2);
//第三种方法创建字典对象 便利构造器创建
NSDictionary *dic3 = [NSDictionary dictionaryWithObjectsAndKeys:@"XiXi",@"X",@"DouDou",@"D",@"DiaoSi",@"S", nil];
NSLog(@"%@",dic3);
//求字典中元素的个数,即键值对(key - value)的个数
NSUInteger dicCount = [dic3 count];
NSLog(@"%lu",dicCount);
//如何取到key对应的value的值
NSString *DValue = [dic3 objectForKey:@"D"];
NSLog(@"DValue = %@",DValue);
NSString *nameValue = [dic objectForKey:@"name"];
NSLog(@"nameValue = %@",nameValue);
//求所有的key的值
NSArray *dic3Keys =[dic3 allKeys];
NSLog(@"dic3Keys = %@",dic3Keys);
NSArray *dicKeys = [dic allKeys];
NSLog(@"keys = %@",dicKeys);
//求所有的values
NSArray *values = [dic3 allValues];
NSLog(@"%@",values);
//for in 遍历字典 实质上遍历得到的是keys
NSMutableArray *allkeys = [NSMutableArray arrayWithCapacity:1];
//设置一个数组,用来存储所有的value
NSMutableArray *allValues = [NSMutableArray arrayWithCapacity:1];
for (id obj in dic) {
//遍历词典得到的是keys
[allkeys addObject:obj];
//通过keys,得到的是values
//将得到的value一一添加到数组当中
NSString *value = [dic objectForKey:obj];
[allValues addObject:value];
}
NSLog(@"allkeys = %@",allkeys);
NSLog(@"allvalues = %@",allValues);
//可变字典
NSMutableDictionary *mutDic = [[NSMutableDictionary alloc]initWithCapacity:1];
NSLog(@"mut Dic = %@",mutDic);
//(1)添加键值对(key - value)
//setObject:forKey 如果我们新添加的键值对此时字典里不存在这个key值的话,则添加这个新的键值对,如果已经存在了key值的话,则会覆盖修改此key值对应value值
[mutDic setObject:@"ChenWeiHong" forKey:@"C"];
NSLog(@"mutDic = %@",mutDic);
[mutDic setObject:@"SongXiaoLong" forKey:@"S"];
NSLog(@"mutDic = %@",mutDic);
[mutDic setObject:@"DongRongJun" forKey:@"D"];
NSLog(@"mutDic = %@",mutDic);
[mutDic setObject:@"MingGe" forKey:@"M"];
NSLog(@"mutDic = %@",mutDic);
//(2)移除键值对,根据key来移除
[mutDic removeObjectForKey:@"S"];
NSLog(@"mutDic = %@",mutDic);
//(3)移除数组,根据传入的key值数组,同时删除多个键值对
[mutDic removeObjectsForKeys:[NSArray arrayWithObjects:@"C",@"D",nil]];
NSLog(@"mutDic = %@",mutDic);
//(4)移除所有键值对 移除所有元素,字典对象所占的空间还是存在的
[mutDic removeAllObjects];
NSLog(@"mutDic = %@",mutDic);
//集合 NSSet 集合也是一个容器,特点:无序性,互异性
//也就是说,集合里面的元素互不相同,而且元素必须是对象类型
NSSet *set = [[NSSet alloc]initWithObjects:@"aa",@"bb",@"cc",@"dd",@"ff",@"dd", nil];
NSLog(@"%@",set);
//求元素个数
NSUInteger setCount = [set count];
NSLog(@"%lu",setCount);
//获取集合里的元素 anyObject 获取任意一个元素(对象)
NSString *element = [set anyObject];
NSLog(@"%@",element);
//可变集合
NSMutableSet *mutSet = [[NSMutableSet alloc] initWithCapacity:1];
//添加集合元素
[mutSet addObject:@"11"];
[mutSet addObject:@"43"];
[mutSet addObject:@"34"];
[mutSet addObject:@"43"];//如果添加已有的元素,则不会添加
//也可以从数组里面添加
[mutSet addObjectsFromArray:[NSArrayarrayWithObjects:@"ew",@"dd",@"fd",@"fs", nil]];
NSLog(@"%@",mutSet);
//删除元素
[mutSet removeObject:@"dd"];
NSLog(@"%@",mutSet);
//计数集合
NSCountedSet *countedSet = [[NSCountedSet alloc]initWithObjects:@"aa",@"bb",@"cc",@"dd",@"ff",@"dd", nil];
//计算dd的个数
NSUInteger ddCount = [countedSet countForObject:@"dd"];
NSLog(@"ddCount = %lu",ddCount);
//字面量 --- "语法糖"/"笑脸语法"
//1.如何将C语言当中的字符串常量快速转化为OC字符串对象
//第一种方法
NSString *str = [[NSString alloc] initWithFormat:@"Hello"];
//第二种方法
NSString *str1 = @"Hello!";
//2.如何将C语言里的基本数据类型快速地转化为OC当中的对象类型(NSNumber对象)
//第一种方法
int a = 10;
NSNumber *numA = [NSNumber numberWithInt:a];
//第二种方法
int b = 8;
NSNumber *numB = @(b);
NSNumber *numB1 = @8;
float c = 10.5;
NSNumber *numC = @(c);
BOOL d = YES;
// NSNumber *bool = @(d);
//3.如何快速创建不可变数组对象
NSArray *array = [[NSArray alloc]initWithObjects:@"xiaolong",@"xiaoming",@"xiaoan",@"xiaoyu", nil];
NSArray *array1 = @[@"xiaolong",@"xiaoming",@"xiaoan",@"xiaoyu"];
NSLog(@"%@",array);
//在使用语法糖的时候,将基本数据类型转化成OC中的对象类型,
NSArray *arr2 = @[@"8",@"24",@"5",@"33"];
//4.如何快速创建可变数组对象
NSMutableArray *mutArr = [NSMutableArrayarrayWithObjects:@"xiaolong",@"xiaoming",@"xiaoan",@"xiaoyu", nil];
//可变数组需要加 mutableCopy ,将不可变数组转化为可变数组
NSMutableArray *mutArr1 = [@[@"xiaolong",@"xiaoming",@"xiaoan",@"xiaoyu"]mutableCopy];
//取到数组中的某一个元素
NSString *arrIndex = [mutArr objectAtIndex:2];
NSString *arrIndex2 = [mutArr objectAtIndex:2];
//5.如何快速创建一个不可变字典
NSDictionary *dic5 = [[NSDictionary alloc]initWithObjectsAndKeys:@"XiaoLong",@"name",@"m",@"sex", @"18",@"age",nil];
NSDictionary *dic6 = @{@"name":@"XiaoLong",@"sex":@"m",@"age":@"18"};
//6.创建一个可变字典
NSMutableDictionary *mutDic6 = [@{@"name":@"XiaoLong",@"sex":@"m",@"age":@"18"} mutableCopy];
//如何通过key值得到相应的value值
NSString *valueOfKey = [mutDic6 objectForKey:@"name"];
NSString *valueOfKey1 = mutDic6[@"name”];
}
return 0;
}