Objective-C:Foundation框架-常用类-NSDictionary
与NSString、NSArray一样,NSDictionary是不可变的,其对应可变类型为NSMutableDictionary。其用法如下:
#import <Foundation/Foundation.h> @interface Student : NSObject
@property (nonatomic, retain) NSString *name; + (id)studentWithName:(NSString *)name;
@end #import "Student.h" @implementation Student + (id)studentWithName:(NSString *)name {
Student *stu = [[Student alloc] init];
stu.name = name;
return [stu autorelease];
} - (void)dealloc {
NSLog(@"%@被销毁了", _name);
// 释放name
[_name release];
[super dealloc];
}
@end
#import <Foundation/Foundation.h>
#import "Student.h" #pragma mark 字典的初始化
void dictCreate() {
// NSDictionary是不可变的
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"v" forKey:@"k"]; // 最常用的初始化方式
dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3", nil]; NSArray *objects = [NSArray arrayWithObjects:@"v1", @"v2", @"v3", nil];
NSArray *keys = [NSArray arrayWithObjects:@"k1", @"k2", @"k3", nil];
dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
NSLog(@"%@", dict);
} #pragma mark 字典的基本用法
void dictUse() {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3", nil]; // count是计算有多少个键值对(key-value)
NSLog(@"count=%zi", dict.count); // 由于NSDictionary是不可变的,所以只能取值,而不能修改值
id obj = [dict objectForKey:@"k2"];
NSLog(@"obj=%@", obj); // 将字典写入文件中
NSString *path = @"/Users/apple/Desktop/dict.xml";
[dict writeToFile:path atomically:YES]; // 从文件中读取内容
dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSLog(@"dict=%@", dict);
} #pragma mark 字典的用法
void dictUse2() {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3", nil];
// 返回所有的key
NSArray *keys = [dict allKeys];
//NSLog(@"keys=%@", keys); NSArray *objects = [dict allValues];
//NSLog(@"objects=%@", objects); // 根据多个key取出对应的多个value
// 当key找不到对应的value时,用marker参数值代替
objects = [dict objectsForKeys:[NSArray arrayWithObjects:@"k1", @"k2", @"k4", nil] notFoundMarker:@"not-found"];
NSLog(@"objects=%@", objects);
} #pragma mark 遍历字典
void dictFor() {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3", nil];
// 遍历字典的所有key
for (id key in dict) {
id value = [dict objectForKey:key];
NSLog(@"%@=%@", key, value);
}
} #pragma mark 遍历字典2
void dictFor2() {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3", nil];
// key迭代器
NSEnumerator *enumer = [dict keyEnumerator];
id key = nil;
while ( key = [enumer nextObject]) {
id value = [dict objectForKey:key];
NSLog(@"%@=%@", key, value);
} // 对象迭代器
// [dict objectEnumerator];
} #pragma mark 遍历字典3
void dictFor3() {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"v1", @"k1",
@"v2", @"k2",
@"v3", @"k3", nil];
[dict enumerateKeysAndObjectsUsingBlock:
^(id key, id obj, BOOL *stop) {
NSLog(@"%@=%@", key, obj);
}];
} #pragma mark
void dictMemory() {
Student *stu1 = [Student studentWithName:@"stu1"];
Student *stu2 = [Student studentWithName:@"stu2"];
Student *stu3 = [Student studentWithName:@"stu3"]; // 一个对象称为字典的key或者value时,会做一次retain操作,也就是计数器会+1
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
stu1, @"k1",
stu2, @"k2",
stu3, @"k3", nil]; // 当字典被销毁时,里面的所有key和value都会做一次release操作,也就是计数器会-1
}
Objective-C:Foundation框架-常用类-NSDictionary的更多相关文章
- Objective-C:Foundation框架-常用类-NSString全解
Foundation框架中常用的类有字符串.集合.字典等,这里介绍字符串NSString.本文分别介绍了NSString的创建.从文件里读取NSString字符串.通过函数改变外部的NSString变 ...
- Objective-C:Foundation框架-常用类-NSObject
NSObject是所有类的基类,其常见用法有: #import <Foundation/Foundation.h> @interface Person : NSObject - (void ...
- Objective-C:Foundation框架-常用类-NSNumber
NSArray.NSDictionary是不可以存储C语言中的基本数据类型的.NSNumber可以将基本数据类型包装成对象,这样可以间接将基本数据类型存进NSArray.NSDictionary等集合 ...
- Objective-C:Foundation框架-常用类-NSMutableDictionary
直接上代码吧: #import <Foundation/Foundation.h> @interface Student : NSObject @property (nonatomic, ...
- Objective-C:Foundation框架-常用类-NSMutableArray
NSMutableArray是NSArray对的子类,它是可变的,可以随意添加或者删除元素.与Array,也有静态和动态两种创建方法.也可已使用NSArray的方法来创建NSMutableArray. ...
- Objective-C:Foundation框架-常用类-NSDate
直接上代码吧: #import <Foundation/Foundation.h> #pragma mark 日期创建 void dateCreate() { // date方法返回的就是 ...
- Objective-C:Foundation框架-常用类-NSNull
集合中是不能存储nil值的,因为nil在集合中有特殊含义,但有时确实需要存储一个表示“什么都没有”的值,那么可以使用NSNull,它也是NSObject的一个子类. #import <Found ...
- Objective-C:Foundation框架-常用类-NSValue
NSNumber是NSValue的子类,前者只能包装数字,后者可以包装任意值.NSArray.NSDictionary只能存储OC对象,不能存储结构体.因此,如果想要在NSArray.NSDictio ...
- Objective-C:Foundation框架-常用类-NSArray
NSArray是用来存储对象的有序列表(NSSet是没有顺序的),它是不可变的.NSArray不能存储C语言中的基本数据类型,如int\float\enum\struct等,也不能存储nil.其用法如 ...
随机推荐
- VSFTP基线安全
在企业级的应用中,越来越多的企业应用开源的vsftpd软件来搭建自己的文件共享服务,优点是速度快且节省开支.然而,企业用户行为难以预料,配置稍有不当则会使该服务成为一个安全风险点,导致带宽恶意占用.用 ...
- 获取DIV与浏览器顶部相聚一定位置之后移动DIV
获取元素(这里定位元素A)距离顶部的高度,接着设定scroll滚动的事件,比如超过那个高度,把A的位置设定为fixed,小于该高度,修改回relative. 方法一: $(function() { ...
- Hibernate级联删除时:Cannot delete or update a parent row: a foreign key constraint fails异常
在删除主表数据时,报了一个异常 Cannot delete or update a parent row: a foreign key constraint fails 原因是主表中还包含字表的数据, ...
- (四)C语言柔性数组、指针赋值
一.柔性数组 今天看了公司的代码,发现一个很奇怪的问题,后来自己写了类似代码,我先把代码贴出来吧. #include<stdio.h> #include<string.h> # ...
- for循环与for in循环
json是js里的一种数据格式.var obj={a:15,b:8,c:12} json数组对象 var arr=[15,8,12]; 数组alert(obj.a); ---15alert(obj[' ...
- eclipse_中的注释_快捷键
eclipse 中的注释 快捷键 把要注释的代码选中,按Ctrl+Shift+/ /* */ 形式的 ctrl+/ //形式的 取消代码注释: 把要注释的代码选中,按Ctrl+Shift+\ /* ...
- UML中类之间的几种关系
类之间可能存在以下几种关系:关联(association).依赖(dependency).聚合(Aggregation,也有的称聚集).组合(Composition).泛化(generalizatio ...
- 【服务器环境搭建-Centos】jdk的安装
1.查看是否已安装openjdk 使用rpm命令查看是否已安装openjdk[root@linuxidc ~]# rpm -qa | grep java tzdata-java-2012c-.el6. ...
- RTC框架
RPC是系统间的一种通信方式,系统间常用的通信方式还有http,webservice,rpc等,一般来讲rpc比http和webservice性能高一些,常见的RPC框架有:thrift,Finagl ...
- kvm相关文章
配置KVM虚拟机的网络 Bridge和Nat方式http://www.it165.net/os/html/201503/12231.html KVM虚拟机网络配置 Bridge方式,NAT方式 htt ...