zoukankan      html  css  js  c++  java
  • Objectc 总结之NSDictionary

    NSDictionary的常见用法总结

            NSArray *array1 = [NSArray arrayWithObjects:@"iphone",@"ipod",nil];

            NSArray *array2 = [NSArray arrayWithObjects:@"mac",@"imac",@"mac pro",nil];

            //类方法初始化自动释放

            NSDictionary *myDictionary = [NSDictionary dictionaryWithObjectsAndKeys:array1,@"mobile",array2,@"computers",nil];//注意用nil结束

            NSLog(@"myDictionary = %@",myDictionary);

            

            int dictSize = [myDictionary count];

            //访问字典中的值

            NSArray *mobile = [myDictionary objectForKey:@"mobile"];

            //从一个对象获取键

            NSArray *keys = [myDictionary allKeysForObject:array1];

            //获取字典中所有值得一个数组

            NSArray *values = [myDictionary allValues];

            //快速枚举

            for(id key in myDictionary)

            {

                NSLog(@"key: %@,value: %@",key,[myDictionary objectForKey:key]);

            }

            //如果字典只包含属性列表对象(NSData,NSDate,NSNumber,NSString,NSArrayNSDictionary)可以保存到文件中

            NSString *filePath = [[[NSBundlemainBundle]resourcePath]stringByAppendingPathComponent:@"dict.txt"];

            BOOL success = [myDictionary writeToFile:filePath atomically:YES];

            //用文件填充

            NSDictionary *myDict2 =[NSDictionary dictionaryWithContentsOfFile:filePath];

            

            //可变字典

            NSMutableDictionary *dictMutable = [[NSMutableDictionary alloc]initWithObjectsAndKeys:array1,@"mobile",array2,@"computer", nil];

            NSString *string4 = @"stringTV";

            //修改对象

            [dictMutable setObject:string4 forKey:@"media"];

            //删除对象

            [dictMutable removeObjectForKey:@"mobile"];

            //删除多个对象

            NSArray *keyArray =[NSArray arrayWithObjects:@"mobile",@"computer", nil];

            [dictMutable removeObjectForKey:keyArray];

            //删除所有对象

            [dictMutable removeAllObjects];

            

  • 相关阅读:
    拉格朗日乘数法
    凸优化
    2018-2-13-安装visualStudio-出现-cant-install-Microsoft.TeamFoundation.OfficeIntegration.Resources...
    2019-11-9-win10-支持默认把触摸提升-Pointer-消息
    2019-7-1-Roslyn-让编译时候-Message-内容默认输出
    2019-8-31-win2d-通过-CanvasActiveLayer-画出透明度和裁剪
    2019-10-4-C#-极限压缩-dotnet-core-控制台发布文件
    2019-8-31-dotnet-获取指定进程的输入命令行
    2019-8-30-PowerShell-通过-WMI-获取系统安装的驱动
    2018-8-10-VisualStudio-2017-项目格式-自动生成版本号
  • 原文地址:https://www.cnblogs.com/superhappy/p/2332947.html
Copyright © 2011-2022 走看看