创建 属性 方法 遍历
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
/**
*
数组:盛放对象的有序容器 index - 对象
例如: 0 -> @"one"
1 -> @"two"
......
字典:盛放对象的无序容器,键值对 键 - 值 ==> 标识(字符串) - 对象
例如: key object
@"键" 对象
@"时宇" @"60分"
......
*/
/*____________(不可变)字典 NSDictionary___________________________________________________*/
//1⃣️创建
//1.实例方法
NSDictionary *dic = [[NSDictionary alloc]init];
NSLog(@"%@",dic);
//2.类方法
NSDictionary *dic1 = [NSDictionary dictionary];
NSLog(@"%@",dic1);
//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
@"two" -> objc2
*/
NSDictionary *dic2 = [[NSDictionary alloc]initWithObjectsAndKeys:objc1,@"第一个",objc2,@"two",objc3,@"叁" ,nil];
// NSDictionary *dic2_ = [NSDictionary dictionaryWithObjectsAndKeys:<#(nonnull id), ...#>, nil]
/**
* 1.字典打印在控制台上,是以{}的形式输出,多个键值对间使用';'相隔
2.形式是 键 = 值 ;
3.文字以Unicode编码显示 http://tool.chinaz.com/tools/unicode.aspx 在线转换
*/
NSLog(@"%@",dic2);
//4.快速创建 键:值,
NSDictionary *dic3 = @{@"a":objc1,@"b":objc2,@"c":objc3};
NSLog(@"%@",dic3);
//5.通过字典创建 ->相当于复制
NSDictionary *dic4 = [[NSDictionary alloc]initWithDictionary:dic3];
// NSDictionary *dic4_ = [NSDictionary dictionaryWithDictionary:dic3];
//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];
NSLog(@"%@",allkeys);
//3.获取所有的value
NSArray *allvalues = [dic4 allValues];
NSLog(@"%@",allvalues);
//4⃣️遍历 : 展示容器内的所有数据
//dic4
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:key]);
}
}
return 0;
}
2016-01-19 19:09:06.628 06NSDictionary[1357:196153] {
}
2016-01-19 19:09:06.631 06NSDictionary[1357:196153] {
}
2016-01-19 19:09:06.632 06NSDictionary[1357:196153] {
two = "<NSObject: 0x1004000f0>";
"\U53c1" = "<NSObject: 0x100400100>";
"\U7b2c\U4e00\U4e2a" = "<NSObject: 0x1004000e0>";
}
2016-01-19 19:09:06.632 06NSDictionary[1357:196153] {
a = "<NSObject: 0x1004000e0>";
b = "<NSObject: 0x1004000f0>";
c = "<NSObject: 0x100400100>";
}
2016-01-19 19:09:06.633 06NSDictionary[1357:196153] count = 3
2016-01-19 19:09:06.633 06NSDictionary[1357:196153] <NSObject: 0x1004000f0>
2016-01-19 19:09:06.634 06NSDictionary[1357:196153] (
a,
b,
c
)
2016-01-19 19:09:06.634 06NSDictionary[1357:196153] (
"<NSObject: 0x1004000e0>",
"<NSObject: 0x1004000f0>",
"<NSObject: 0x100400100>"
)
2016-01-19 19:09:06.634 06NSDictionary[1357:196153] <NSObject: 0x1004000e0>
2016-01-19 19:09:06.634 06NSDictionary[1357:196153] <NSObject: 0x1004000f0>
2016-01-19 19:09:06.634 06NSDictionary[1357:196153] <NSObject: 0x100400100>
2016-01-19 19:09:06.634 06NSDictionary[1357:196153] <NSObject: 0x1004000e0>
2016-01-19 19:09:06.635 06NSDictionary[1357:196153] <NSObject: 0x1004000f0>
2016-01-19 19:09:06.702 06NSDictionary[1357:196153] <NSObject: 0x100400100>
Program ended with exit code: 0
每天记住关于iOS的十个单词 6:48 - - 7:10
Foundation //创建 基础 根据 地基
string //细绳 一串 牵线
mutable //可变的
array //大批 数组 服装
dictionary //词典
null //无效的
set //放置 安置 确定 设定
value//价值 值得 有用
data //数据
format //样式 制式 格式
Day.01.19 Foundation不可变字典
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- //NSMutableDictionary* params = [NSMutableDictionary dict...