oc 中的 NSDictionary 的作用同 java 中的字典类相同,提供了 “键-值”对的组合。比如,是用字典类实现对学生姓名和学号的存放,编号是一个键(唯一性),姓名是值。它的方法有:

  

  下面通过例子来具体说明它的用法:

   1)构建字典

  

 #import <Foundation/Foundation.h>

 int main(int argc , const char *argv[]){
@autoreleasepool{
NSDictionary *dicti1 = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"",@"lisi",@"",@"wangwu",@"", nil];
NSEnumerator *keynum = [dicti1 keyEnumerator];
for(NSString *key in keynum)
NSLog(@"key = %@ , value = %@",key,[dicti1 objectForKey:key]); NSLog(@"----------------------------");
NSDictionary *dicti2 = [[NSDictionary alloc]initWithObjectsAndKeys:@"zhangsan",@"",@"lisi",@"",@"wangwu",@"", nil];
keynum = [dicti2 keyEnumerator];
for(NSString *key in keynum)
NSLog(@"key = %@ , value = %@",key,[dicti2 objectForKey:key]); NSLog(@"----------------------------");
NSDictionary *dicti3 = @{@"":@"zhangsan",@"":@"lisi",@"":@"wangwu"};
keynum = [dicti3 keyEnumerator];
for(NSString *key in keynum)
NSLog(@"key = %@ , value = %@",key,[dicti3 objectForKey:key]); NSLog(@"------------------------------");
NSArray *keyarray = @[@"",@"",@""];
NSArray *valuearray = @[@"zhangsan",@"lisi",@"wangwu"];
NSDictionary *dicti4 = [NSDictionary dictionaryWithObjects:valuearray forKeys:keyarray];
keynum = [dicti4 keyEnumerator];
for(NSString *key in keynum)
NSLog(@"key = %@ , value = %@",key,[dicti4 objectForKey:key]); }
return ;
}

  

  2)字典的遍历

  

 #import <Foundation/Foundation.h>

 int main(int argc , const char *argv[]){
@autoreleasepool {
NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"",@"lisi",@"",@"wangwu",@"", nil];
NSEnumerator *number = [dicti keyEnumerator];
for(NSString *key in number)
NSLog(@"key = %@ , value = %@",key,[dicti objectForKey:key]); NSLog(@"----------------------------");
NSArray *keyarray = [dicti allKeys];
for(NSString *key in keyarray)
NSLog(@"key = %@ , value = %@",key,[dicti objectForKey:key]); NSLog(@"-----------------------------");
for(NSString *key in dicti)
NSLog(@"key = %@ , value = %@",key,[dicti objectForKey:key]); NSLog(@"-----------------------------");
[dicti enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSLog(@"key = %@ ,value = %@",key,obj);
}];
}
    return 0;
}

3)  文件的读取

 #import <Foundation/Foundation.h>

 int main(int argc , const char *argv[]){
@autoreleasepool {
NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"",@"lisi",@"",@"wangnwu",@"",nil]; NSString *filename = @"dicti.txt";
BOOL iswrite = [dicti writeToFile:filename atomically:YES];
if(iswrite)
NSLog(@"write to file ok");
else
NSLog(@"write to file error"); NSDictionary *dicti2 = [NSDictionary dictionaryWithContentsOfFile:filename];
NSLog(@"dicti2 = %@",dicti2); }
    return 0;
}

  4) 查找

  

 #import <Foundation/Foundation.h>

 int main(int argc , const char *argv[])
{
@autoreleasepool {
NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"",@"lisi",@"",@"wangwu",@"",@"zhangsan",@"",nil]; NSArray *keyarray = [dicti allKeys]; // 查找所有键值
NSLog(@"%@",keyarray); NSArray *valuearray = [dicti allValues]; // 查找所有值
NSLog(@"%@",valuearray); NSString *value = [dicti objectForKey:@""]; // 按键值查找
NSLog(@"value = %@",value); value = dicti[@""];
NSLog(@"value = %@",value); NSArray *keyarray2 = @[@"",@"",@""]; // 利用数组的方式查找
NSArray *vlauearray2 = [dicti objectsForKeys:keyarray2 notFoundMarker:@"nil"];
NSLog(@"vlauearray2 = %@",vlauearray2); NSArray *keyarray3 = [dicti allKeysForObject:@"zhangsan"];
NSLog(@"keyarray3 = %@",keyarray3); NSDictionary *dicti2 = [dicti dictionaryWithValuesForKeys:keyarray2];
NSLog(@"dicti2 = %@",dicti2);
}
return ;
}

5) 排序

  

 #import <Foundation/Foundation.h>

 int main(int argc,char **argv){
@autoreleasepool {
NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"",@"lisi",@"",@"wangwu",@"",@"zhangsan",@"",nil];
NSLog(@"dicti = %@",dicti); NSDictionary *dicti2 = [NSDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@,@"lisi",@,@"wangu",@, nil];
NSLog(@"dicti2 = %@",dicti2); NSArray *array = [dicti2 keysSortedByValueUsingSelector:@selector(compare:)];
for(NSNumber *key in array)
NSLog(@"key = %@,value = %@",key,dicti2[key]);
}
return ;
}

  2. 同样字典对象中也存在可变字典(NSDictionary),下面我们来通过一个例子来说明它的用法:

  

  代码举例:

  

 #import <Foundation/Foundation.h>

 int main(int argc,char **argv){
@autoreleasepool {
NSMutableDictionary *mdicti = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"zhangsan",@"",@"lisi",@"",@"wangwu",@"",nil]; [mdicti setValue:@"zhaoliu" forKey:@""]; // 如果 key 不存在就添加,key 存在就修改
NSLog(@"mdicti = %@",mdicti); [mdicti setValue:@"zzz" forKey:@""];
NSLog(@"mdicti = %@",mdicti); mdicti[@""] = @"bbb";
NSLog(@"mdicti = %@",mdicti); NSDictionary *dicti = [NSDictionary dictionaryWithObjectsAndKeys:@"aaa",@"",@"bbb",@"",@"ccc",@"",nil];
[mdicti addEntriesFromDictionary:dicti];
NSLog(@"dicti = %@",mdicti); [mdicti removeObjectForKey:@""]; // 删除
NSLog(@"mdicti = %@",mdicti); NSArray *array = @[@"",@"",@""];
[mdicti removeObjectsForKeys:array];
NSLog(@"mdicti = %@",mdicti); [mdicti removeAllObjects]; // 删除所有的
NSLog(@"%@",mdicti);
}
return ;
}

Objective-c 字典对象的更多相关文章

  1. VBS使用Scripting.Dictionary字典对象

    Scripting.Dictionary是个很有用的组件,其创建了类似于Key索引对应Value值的字典对象,并且在其内部提供了快速索引访问的机制,可以让我们通过Key直接索引到指定的Value,比遍 ...

  2. python 基础学习(字典对象,set对象)

    1.dict 字典对象 a.定义对象 d={'a':14,'b':12}b.通过key获取value d['a'] 方法1.判断key是否存在 if 'a' in d: d['a']方法2:通过用ge ...

  3. JavaScript中创建字典对象(dictionary)实例

    这篇文章主要介绍了JavaScript中创建字典对象(dictionary)实例,本文直接给出了实现的源码,并给出了使用示例,需要的朋友可以参考下 对于JavaScript来说,其自身的Array对象 ...

  4. python爬虫requests json与字典对象互相转换

    import requests import json ''' json.loads(json_str) json字符串转换成字典 json.dumps(dict) 字典转换成json字符串 ''' ...

  5. 有一个字典对象,d = {'a':1,'b':2},请用尽量简洁的代码将d转换成{1: 'a', 2: 'b'}

    题目:有一个字典对象,d = {'a':1,'b':2},请用尽量简洁的代码将d转换成{1: 'a', 2: 'b'} 第一种方法: d = {'a': 1, 'b': 2}d = {value: k ...

  6. python json与字典对象互相转换

    改文章转自:https://www.cnblogs.com/Lin-Yi/p/7640147.html 1 import requests 2 import json 3 ''' 4 json.loa ...

  7. 字典对象的 Pythonic 用法(上篇)

    字典对象在Python中作为最常用的数据结构之一,和数字.字符串.列表.元组并列为5大基本数据结构,字典中的元素通过键来存取,而非像列表一样通过偏移存取.笔者总结了字典的一些常用Pyhonic用法,这 ...

  8. 字典对象的 Pythonic 用法(上篇:转载)

    转载:https://mp.weixin.qq.com/s?timestamp=1498528588&src=3&ver=1&signature=DfFeOFPXy44ObCM ...

  9. iOS :Object-C 语言merge两个字典对象

    Object-C 语言merge两个字典对象 - (id)mutableDictionaryCopyIfNeeded:(id)dictObj { if ([dictObj isKindOfClass: ...

随机推荐

  1. Delphi中的Rtti函数

    TTypeKind,类型类别,tkclass,tkinteger,tkstring等.类,属性都是一种类型. ttypedata,是一个record包括ttypekind.是一个类的描述.TTypeK ...

  2. 读配置文件 properties

    /** * */package com.sprucetec.tms.fee.utils;import java.io.IOException;import java.util.ArrayList;im ...

  3. IE8 多进程问题

    IE8的一个重要特性就是每个Tab(选项卡)在独立的进程中运行,我们称之为LCIE(Loosely-Coupled IE). 所以大家在升级到IE8之后会发现资源管理器里面有两个或者多个iexplor ...

  4. poj1665

    #include <stdio.h> #include <stdlib.h> #define pi 3.1415926 int main() { float dia,tim; ...

  5. 【FZU】2152 文件系统

     Problem 2152 文件系统 Accept: 63    Submit: 126 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Probl ...

  6. 解析配置文件ConfigParser模块

    一.ConfigParser简介 ConfigParser 是用来读取配置文件的包.配置文件的格式如下:中括号“[ ]”内包含的为section.section 下面为类似于key-value 的配置 ...

  7. 【菜鸟学习Linux】-第三章- Linux环境搭建-使用VMware9安装Ubuntu 12.04系统

    上一步,我们安装了VMware9虚拟机,现在我们就是用它来安装Ubuntu12.04系统,至于Ubuntu是什么,我就不废话了,大家google一下,比我讲的清楚,好了,开始干活! Ubuntu官网下 ...

  8. Swift学习笔记:类和结构

    一.类和结构的异同 类和结构有一些相似的地方.它们都能够: 1. 定义一些能够赋值的属性: 2. 定义具有功能性的方法 3. 定义下标.使用下标语法 4. 定义初始化方法来设置初始状态 5. 在原实现 ...

  9. JS关闭页面无提示

    window.opener=null; window.open('','_self'); window.close();

  10. js中的数组和字符串的一些方法

    数组的一些方法: 1.join()和split()方法 <script type="text/javascript">var x;var a=new Array();a ...