______________________________不可变字典________________________________________
数组:盛放对象的有序容器 index - 对象
例如: 0 -> @"one"
2 -> @"two"
......
字典:盛放对象的无需容器,键值对 键 - 值 ==>标示(字符串) - 对象
例如: key object
@"键" 对象
@"小明" @"60分"
......
//1.创建
//1.实例方法
NSDictionary *dic = [[NSDictionary alloc]init];
//2.类方法
NSIC提哦那容易*dic1 = [[NSDictionary]dictionary];
//3.通过键值对创建
//NSArray *array = [NSArray arrayWithObjects:@"1",@"2",nil];
NSObject *objc1 = [[NSObject alloc]init];
NSObject *objc2 = [[NSObject alloc]init];
NSObject *objc3 = [[NSObject alloc]init];
/*
@"叁" -> objc3
@"第一个" -> objc1
@"tow" -> objc2
*/
NSDictionary *dic2 = [[NSDictionary alloc]initWithObjectsAndKeys:objc1,@"第一个",objc2,@"tow",objc3,@"叁",nil];
//NSDictionary *dic2_ = [NSDictionary dictionaryWithObjectsAndKeys:(nonnull id),...,nil]
/*
1.字典打印在控制台上,是以{}的形式输出,多个键值对间使用';'相隔
2.形式是 键 = 值;
2.文字以Unicode编码显示 (可以在线转换)
*/
NSLog(@"%@",dic2);
//4.快速创建 键:值,
NSDictionary *dic3 = @{@"a":objc1,@"b":bojc2,@"c":objc3};
NSLog(@"%@",dic3);
//5.通过字典创建 -> 相当于赋值
NSDictionary *dic4 = [[NSDictionary alloc]initWithDictionry:dic3];
//NSDictionary *dic4_ = [NSDictionary dictionaryWithDictionary:dic3];
//2.属性
NSLog(@"count = %ld",[dic4 count]);
NSLog(@"count = %ld",dic4.count);
//2.属性
//NSLog(@"count = %ld",[dic4 count]);
NSLog(@"count = %ld",dic4.count);
3.方法
//1.获取字典中的对象
NSObject *objcB = [dic4 objectForKey:@"b"];
NSLog(@"%@",objcB);
//2.获取所有的key❗️常用
NSArray *allkeys = [dic4 allKeys];
NSLo(@"%@",allkeys);
//3.获取所有value
NSArray *allvalues = [dic4 allvalues];
NSLog(@"%@",allvalues);
//4.遍历:展示容器内的所有数据
NSArray *keys = [dic4 allKeys];
for(int i = 0;i < dic4.count;i++)
{
NSString *key = [keys objectAtIndex:i];
/*_____________________________________________*/
NSLog(@"%@",[dic4 objectForKey:key]);
}
for(NSString *key in keys)
{
NSLog(@"%@",[dic4 objectForKey:])
}
网友评论