关闭

【黑马程序员】OC-Foundation框架—NSDictionary和NSMutableDictionary

标签: Foundation框架NSDictionaryiosocNSMutableDictionary
141人阅读 评论(0) 收藏 举报
分类:

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


一、NSDictionary介绍


1)什么是NSDictionary

NSDictionary翻译过来叫做”字典”

日常生活中, “字典”的作用: 通过一个拼音或者汉字, 就能找到对应的详细解释

NSDictionary的作用类似: 通过一个key(键), 就能找到对应的value(值) NSDictionary是不可变的, 一旦初始化完毕, 里面的内容就无法修改


<span style="font-size:14px;">//
//  main.m
//  NSDictionary介绍
//
//  Created by Brisinga on 15/11/18.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //创建空字典
        NSDictionary *d1 = [NSDictionary dictionary];
        //创建只有一组键值对的字典
        NSDictionary *d2 = [NSDictionary dictionaryWithObject:@"zhangsanfeng" forKey:@"jsf"];
        //创建多组键值对的字典
        NSDictionary *d3 = [NSDictionary dictionaryWithObjectsAndKeys:@"hufei",@"hf",@"leifeng",@"lf", nil];
        //打印字典
        NSLog(@"%@",d3);
        
        //快速创建一个字典
        NSDictionary *d4 = @{@"zs":@"zhangsan",@"ls":@"lisi"};
        NSLog(@"%@",d4);
        
        //打印字典长度
        NSLog(@"%ld",d4.count);
        
        //根据key值取出value值
        NSLog(@"%@",[d4 objectForKey:@"ls"]);
        NSLog(@"%@",d4[@"zs"]);
        
        //字典的遍历
        //方法一  //获取的是对应的key
        for (NSString *str in d4) {
            NSLog(@"%@",d4[str]);
        }
        //方法二
        [d4 enumerateKeysAndObjectsUsingBlock:^(id   key, id   obj, BOOL * _Nonnull stop) {
            NSLog(@"%@ = %@",key,obj);
        }];
        
    }
    return 0;
}
</span>

二、NSDictionary的简写以及文件操作

<span style="font-size:14px;">//
//  main.m
//  NSDictionary的简写以及文件操作
//
//  Created by Brisinga on 15/11/18.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //简写创建dictionary
        NSDictionary *d = @{@"zbz":@"zhangbozhi",@"cgx":@"chenguanxi",@"hl":@"hello"};
        
        //简写形式获取key对应的value
        NSString *str = d[@"zbz"];
        NSLog(@"%@",str);
        
        //把字典保存到文件中
        [d writeToFile:@"/Users/Brisinga/Desktop/a.plist" atomically:YES];
        
        //从文件中读取字典
        NSDictionary *d2 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/Brisinga/Desktop/a.plist"];
        NSLog(@"%@",d2);
        
        //应用:
        //省市
        //辽宁   大连
        //       沈阳
        //河北    保定
        //       石家庄
        
        NSArray *arr1 = @[@"dalian",@"shenyang"];
        NSArray *arr2 = @[@"baoding",@"shijiazhuang"];
        NSDictionary *d3 = @{@"ln":arr1,@"hb":arr2};
        [d3 writeToFile:@"/Users/Brisinga/Desktop/a.plist" atomically:YES];
        //打印字典
        for (NSString *key in d3) {
            NSLog(@"%@",d3[key]);
        }
        //方法二
        [d3 enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {
            for (NSString *str in obj) {
                NSLog(@"%@:%@",key,str);
            }
        }];
        //获取每一个数组
        arr1 = d3[@"ln"];
        NSLog(@"%@",arr1);
    }
    return 0;
}
</span>

三、NSMutableDictionary的介绍和使用


<span style="font-size:14px;">//
//  main.m
//  NSMutableDictionary的介绍和使用
//
//  Created by Brisinga on 15/11/18.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        //1)可变字典的创建
        NSMutableDictionary *d1 = [NSMutableDictionary dictionary];
        NSMutableDictionary *d2 = [NSMutableDictionary dictionaryWithCapacity:5];
        
        //2)可变字典添加键值对
        [d1 setObject:@"zhangbozhi" forKey:@"zbz"];
        [d1 setValue:@"liuneng" forKey:@"ln"];
        NSLog(@"%@",d1);
        //3)删除
        [d1 removeObjectForKey:@"zbz"];
        //4)修改
        [d1 setValue:@"zhang" forKey:@"zbz"];
        [d1 setObject:@"liaoning" forKey:@"ln"];
        d1[@"ln"] = @"llllnnn";
        NSLog(@"%@",d1);
        //5)查找
        NSArray *arr = [d1 allKeys];  //获取所有key
        if([arr containsObject:@"ln"]){
            NSLog(@"存在");
        }
    }
    return 0;
}</span>


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5782次
    • 积分:259
    • 等级:
    • 排名:千里之外
    • 原创:21篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档