zoukankan      html  css  js  c++  java
  • Objective-C( Foundation框架 一 NSDictionary (NSMutaleDictionary))

    NSDictionary

    不可变的字典

    创建字典的方法

            // 创建字典的方式
            NSDictionary *dy = [NSDictionary dictionaryWithObject:@"a"forKey:@"b"];
            NSDictionary *dy1 = [NSDictionary dictionaryWithObjectsAndKeys:@"a",@"b",@"c",@"d" ,nil];
            
            // 快速创建字典
            // 重复key值,重复的不能保存到字典中
            NSDictionary *dy2 = @{@"we":@"haha", @"we":@"ha"};
            
            NSLog(@"%@",dy);
            NSLog(@"%@",dy1);
            NSLog(@"%@",dy2);
    

     获取字典的长度

            // 获取字典长度
            NSDictionary *dy2 = @{@"we":@"haha", @"w":@"ha"};
            NSLog(@"%lu",dy2.count); // 输出2   
            // 如果key值重复,长度不计算在内
            NSDictionary *dy2 = @{@"we":@"haha", @"we":@"ha"};
            NSLog(@"%lu",dy2.count); //输出1 

    字典的遍历

            NSDictionary *dy1 = [NSDictionary dictionaryWithObjectsAndKeys:@"a",@"b",@"c",@"d" ,nil];
            
            // 快速创建字典
            // 重复key值,重复的不能保存到字典中
            NSDictionary *dy2 = @{@"we":@"haha", @"w":@"ha"};
            NSLog(@"%lu",dy2.count);
            
            // 字典的遍历
            for (NSString *key in dy2) {
                NSLog(@"key = %@, value = %@", key, [dy2 objectForKey:key]);
            }
            // 枚举类型遍历
            [dy1 enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
                NSLog(@"key = %@, value = %@", key, obj);
            }];

    把字典存到文件中

            // 把字典保存到文件中
            NSDictionary *dy2 = @{@"we":@"haha", @"w":@"ha"};
            BOOL isWrite = [dy2 writeToFile:@"/Users/cloudwalk/Desktop/test.plist" atomically:YES];
            if (isWrite) {
                NSLog(@"写入成功");
            }

    从文件中读取字典

            NSDictionary *readDy2 = [NSDictionary dictionaryWithContentsOfFile:@"/Users/cloudwalk/Desktop/test.plist"];
            NSLog(@"%@",readDy2);
            

    通过把数组添加到字典中,构建成数组字典

            NSArray *sdArr = [NSArray arrayWithObjects:@"zaozhuang",@"jinan",nil];
            NSArray *jxArr = [NSArray arrayWithObjects:@"jiujiang",@"nanchang",nil];
            NSDictionary *citys = [NSDictionary dictionaryWithObjectsAndKeys:sdArr,@"sd",jxArr,@"jx", nil];
            NSLog(@"citys = %@",citys);
            NSDictionary *citys = [NSDictionary dictionaryWithObjectsAndKeys:sdArr,@"sd",jxArr,@"jx", nil];
            NSLog(@"citys = %@",citys);
            // 把citys存到文件中
            [citys writeToFile:@"/Users/cloudwalk/Desktop/test1.plist" atomically:YES];

    NSMutableDictionay

        // 创建可变字典
        NSMutableDictionary *dy1 = [NSMutableDictionary dictionary];
        
        //  添加键值对
        [dy1 setValue:@"lili" forKey:@"is"];
        [dy1 setValue:@"heihei" forKey:@"hah"];
        NSLog(@"%@",dy1);
        
        // 删除某个键值对
        //[dy1 removeObjectForKey:@"is"];
        NSLog(@"%@",dy1);
        
        // 快速修改键值对
        dy1[@"is"] =@"xossk";
        NSLog(@"%@",dy1);
        
        // 修改键值对
        [dy1 setObject:@"xixi" forKey:@"is"];
        NSLog(@"%@",dy1);
  • 相关阅读:
    Java基础——clone()方法浅析
    Unity shader error: “Too many texture interpolators would be used for ForwardBase pass”
    ar 解压一个.a文件报错: xxx.a is a fat file (use libtool(1) or lipo(1) and ar(1) on it)
    How to set up "lldb_codesign" certificate!
    Unity-iPhone has Conflicting Provisioning Settings
    ETC1/DXT1 compressed textures are not supported when publishing to iPhone
    Xcode 提交APP时遇到 “has one iOS Distribution certificate but its private key is not installed”
    XCode iOS之应用程序标题本地化
    苹果电脑(Mac mini或Macbook或iMac)恢复出厂设置
    Unity 4.7 导出工程在XCode10.1上编译报错
  • 原文地址:https://www.cnblogs.com/1023843587qq/p/4799689.html
Copyright © 2011-2022 走看看