Object-C-NSDictionary
存储对象都必须是id(对象类型)不能使基础类型
NSDictionary *scores=[[NSDictionary alloc]initWithObjectsAndKeys:@"89",@"english",@"70",@"computer",nil];
*scores=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:89],@"english"];
scores=[NSDictionary dictionaryWithObjectsAndKeys:]用法与initWithObjectsAndKeys;
NSNumber *englishScore=[scores objectForKey:@"english"];
集合的遍历
方法一
for(NSString *key int socres)
{
// 通过每个元素的key访问value
NSLog(@"%@:%d",key,[[score objetForKey:key] intValue]);
}
方法二
[socres enumerateKeysAndObjectsUsingBloc:^(id key, id obj,BOOL *stop)
{
NSNumber *num=(NSNumber *)obj;
NSLog(@"%@:%d",key,[num intValue]);
}
方法三
NSArray *keysArray = [scores allKeys];
for(int i=0; i<[scores count]; i++)
{
NSLog(@"%@:%d",[keysArray objectAtIndex:i);
NSLog(@"")----;
}
字典排序
NSArray *keysArray=[scores keysSortedByValueUsingSelector:@selector(compare:)];
字典:
//key 一般使用字符串
//value 可以使用任意对象类型
// NSDictionary *scores = [[NSDictionary alloc]initWithObjectsAndKeys:@"89",@"english",@"70",@"computer", nil];
//
// NSLog(@"%@",scores); NSDictionary *scores2=[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:],@"english",
[NSNumber numberWithInt:],@"maths",
[NSNumber numberWithInt:],@"computer"
, nil]; // NSLog(@"%ld",[scores2 count]);
// NSLog(@"%@",scores2);
NSNumber *englishScores = [scores2 objectForKey:@"english"];
NSLog(@"%@",englishScores);
NSLog(@"%d",[englishScores intValue]);
// //遍历方法1:for in
for (NSString *key in scores2) {
//通过每个元素的key访问value
NSLog(@"%@:%d",key,[[scores2 objectForKey:key] intValue]);
}
// //遍历方法2:for
NSArray *keysArray = [scores2 allKeys];
for (int i=; i<[scores2 count]; i++) {
NSString *key = [keysArray objectAtIndex:i];
NSLog(@"%@:%d",key,[[scores2 objectForKey:key]intValue]);
}
//
//排序
NSArray *keys = [scores2 keysSortedByValueUsingSelector:@selector(compare:)];
NSLog(@"%@",keys);
}
return ;
ie
Object-C-NSDictionary的更多相关文章
- JSON Object(如NSDictionary,NSArray)转化为JSON格式的NSString #iOS开发
NSString *string = [self jsonObjectToJSONString:inputDataDic]; -(NSString*)jsonObjectToJSONString:(i ...
- 如何删除NSDictionary或NSArray中的NSNull
前段时间与某公司的技术交流,被问到一个问题,如何删除NSDictionary中的NSNull.当时在纸上写,以前太依赖Xcode编译器了,以至于方法名都写不全,最终也没写出来,我想我肯定被鄙视的体无完 ...
- Foundation框架之NSArray、NSDictionary、NSSet及其Mutable类型
Foundation框架之NSArray.NSDictionary.NSSet及其Mutable类型 目录 概述——对Foundation框架集合类的理解 NSArray NSDictionary N ...
- NSDictionary和NSMaptable, NSArray,NSSet,NSOrderedSet和NSHashTable的区别
NSSet, NSDictionary, NSArray是Foundation框架关于集合操作的常用类, 和其他标准的集合操作库不同, 他们的实现方法对开发者进行隐藏, 只允许开发者写一些简单的代码, ...
- [Objective-C] 009_Foundation框架之NSDictionary与NSMutableDictionary
在Cocoa Foundation中NSDictionary和NSMutableDictionary 用于对象有序集合,NSDictionary和NSMutableDictionary 与 NSArr ...
- 【原】AFNetworking源码阅读(三)
[原]AFNetworking源码阅读(三) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇的话,主要是讲了如何通过构建一个request来生成一个data tas ...
- iOS:以前笔记,未整理版。太多了,先放着吧。。。。。。。
1. -(void)timetick { _d = 0; NSTimer *newtime =[NSTimer scheduledTimerWithTimeInterval:1 target:self ...
- iOS开发系列--音频播放、录音、视频播放、拍照、视频录制
--iOS多媒体 概览 随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制, ...
- iOS---观察者模式之--->KVO
文章结构如下: Why? (为什么要用KVO) What? (KVO是什么) How? ( KVO怎么用) More (更多细节) 原理 自己实现KVO 在我的上一篇文章浅谈 iOS Notifica ...
- AFNetworking 3.0 源码解读(三)之 AFURLRequestSerialization
这篇就讲到了跟请求相关的类了 关于AFNetworking 3.0 源码解读 的文章篇幅都会很长,因为不仅仅要把代码进行详细的的解释,还会大概讲解和代码相关的知识点. 上半篇: URI编码的知识 关于 ...
随机推荐
- Cracking the Coding Interview(linked list)
第二章的内容主要是关于链表的一些问题. 基础代码: class LinkNode { public: int linknum; LinkNode *next; int isvisit; protect ...
- 用js内置对象XMLHttpRequest 来用ajax
步骤: /* 用XMLHTTPRequest来进行ajax异步数据交交互*/ 主要有几个步骤: //1.创建XMLHTTPRequest对象 //最复杂的一步 if (window.XMLHttpRe ...
- intellij idea如何快速格式化代码
选中代码,一键格式化代碼: Ctrl+Alt+L
- IDEA安全编码组件
import java.io.UnsupportedEncodingException;import java.security.Key;import java.security.Security; ...
- thinkCMF----增删改查操作
thinkCMF的增删改查基本操作: 一.增加数据 $res = Db::name('form')->insert($data); 示例代码: public function index(){ ...
- Win7去掉桌面图标小箭头
去掉win7的快捷方式的小箭头: 每当我们装完一个软件,在桌面生成快捷方式的时候总会有个小箭头,有些朋友看到觉得很烦,如何去掉这个小箭头呢? 点击开始图标 - 附件 - 命令提示符(有情提示,请右击用 ...
- FileStream实现多线程断点续传(已封装)
处理文件分片 处理缺失的分片文件 合并分片文件 MD5验证文件 using System; using System.Collections.Generic; using System.IO; usi ...
- saltstack------安装篇
一.环境准备 系统:centos7 and centos6.8 IP 系统 172.16.1.10 centos7 172.16.1.11 centos7 172.16.1.21 centos6. ...
- jsonk可以传递boolean
JSON 布尔值可以是 true 或者 false.如: { "flag":true } 或者{“flag”,"true"}
- 计蒜客 31447 - Fantastic Graph - [有源汇上下界可行流][2018ICPC沈阳网络预赛F题]
题目链接:https://nanti.jisuanke.com/t/31447 "Oh, There is a bipartite graph.""Make it Fan ...