- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
-
- //在字典中汉字用utf8格式储存
- NSDictionary *dict1=[NSDictionary dictionaryWithObject:@"张三" forKey:@"zs"];
- NSLog(@"%@",dict1);
-
- NSDictionary *dict2=[NSDictionary dictionaryWithObjectsAndKeys:@"詹桑",@"zs",@"温暖",@"wh", nil];
-
- NSLog(@"%@",dict2);
- }
- //快速创建
- NSDictionary *dict3=@{@"zs":@"zhangsna",@"ls":@"lisi"};
- NSLog(@"%@",dict3);
-
- //objectForKey 根据k值获取value值
- NSString *s1= [dict3 objectForKey:@"zs"];
- NSLog(@"%@",s1);
- //dict3.count 获取字典元素个数
- NSLog(@"%lu",dict3.count);
- //遍历数组 快速枚举
- for (NSString *key in dict3) {
-
- NSLog(@"key=%@,value=%@",key,[dict3 objectForKey:key]);
- }
-
- //bock 注意[{ }]格式
- [dict3 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
- {
- NSLog(@"%@%@",key,obj);
- }];
-
- NSDictionary *dictionary=
- @{@"zhangsan":@"zs",@"ww":@"wangwu",@"xr":@"xiaoruo"};
- //快速获取k值对应的value值
- NSLog(@"%@",dictionary[@"ww"]);
- bool b= [dictionary writeToFile:@"/Users/mac/Desktop/123.plist" atomically:YES];
- if (b) {
- NSLog(@"写入成功");
- }
-
- NSDictionary *d2=[NSDictionary dictionaryWithContentsOfFile:@"/Users/mac/Desktop/123.plist"];
- NSLog(@"%@",d2);
-
- //将字典里的元素存放到数组
- // NSArray *a=dictionary[@"xr"];
- // NSLog(@"%@",[a lastObject]);
-
-
-
-
-
- }
复制代码
- #import <Foundation/Foundation.h>
- int main(int argc, const char * argv[]) {
- @autoreleasepool {
- //可变字典才可以改xxxx
- // NSDictionary *dict=[NSDictionary dictionaryWithObjectsAndKeys:@"1":@"!!!",@"2":@"222", nil];
- NSMutableDictionary *dict=[NSMutableDictionary dictionary];
- //增加
- [dict setValue:@"333" forKey:@"3"];
- [dict setValue:@"312" forKey:@"2"];
- [dict setValue:@"3da3" forKey:@"1"];
- NSLog(@"%@",dict);
- //修改
- [dict setObject:@"222" forKey:@"3"];
- NSLog(@"%@",dict);
- // 查找 allKeys显示所有k
- NSArray *arr= [dict allKeys];
- if ([arr containsObject:@"1"]) {
- NSLog(@"已找到");
- }
-
-
-
- }
- return 0;
- }
复制代码
|
|