NSDictionary等基本类型的使用方法

NSDictionary *dict;

for(NSString * akey in dict)

{

  //........

}很好用

1.创建不可变词典

[NSDictionary dictionaryWithObjectsAndKeys:..] : 使用键值对儿直接创建词典对象,结尾必需使用nil标志结束

[NSDictionary initWithObjectsAndKeys:..] :使用键值对儿初始化词典对象,结尾必需使用nil标志结束。

[dictionary count]: 得到词典的长度单位。

[dictionary keyEnumerator]: 将词典的所有KEY储存在NSEnumerator中,NSEnumerator很像Java语言 中的迭代器,使用快速枚举可以遍历词典中所有储存KEY值。

[dictionary  objectEnumerator]: 将词典的所有value储存在NSEnumerator中,用法和上面差不多可用来遍历KEY对应储存的Value值。

[dictionary objectForKey:key]: 通过传入KEY对象可以拿到当前KEY对应储存的值。

复制代码
#import <UIKit/UIKit.h>
#import "MyClass.h"
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//添加我们的测试代码

NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"雨松MOMO",@"name",@"15810463139",@"number", nil];

//得到词典的数量
int count = [dictionary count];
NSLog(@"词典的数量为: %d",count);

//得到词典中所有KEY值
NSEnumerator * enumeratorKey = [dictionary keyEnumerator];

//快速枚举遍历所有KEY的值
for (NSObject *object in enumeratorKey) {
NSLog(@"遍历KEY的值: %@",object);
}

//得到词典中所有Value值
NSEnumerator * enumeratorValue = [dictionary objectEnumerator];

//快速枚举遍历所有Value的值
for (NSObject *object in enumeratorValue) {
NSLog(@"遍历Value的值: %@",object);
}

//通过KEY找到value
NSObject *object = [dictionary objectForKey:@"name"];

if (object != nil) {
NSLog(@"通过KEY找到的value是: %@",object);
}



int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
复制代码

 

2.创建可变词典对象

NSMutableDictionary 是NSDictionary的子类,所以继承了NSDictionary的方法。

[NSMutableDictionary dictionaryWithCapacity:10] : 创建一个可变词典初始指定它的长度为10.,动态的添加数据如果超过10这个词典长度会自动增加,所以不用担心数组越界。推荐用这种方式

[NSMutableDictionary initWithCapacity:10]  :只是初始化一个词典的长度为10。

[dictionary setObject:@"雨松MOMO" forKey:@"name"] :向可变的词典动态的添加数据 ,这里的key是name ,值是雨松MOMO如果词典中存在这个KEY的数据则直接替换这个KEY的值。(易混的地方,慎重!)

[dictionary removeAllObjects..] : 删除掉词典中的所有数据。

[dictionary removeObjectForKey..] :删除掉词典中指定KEY的数据 。

复制代码
#import <UIKit/UIKit.h>
#import "MyClass.h"
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

//添加我们的测试代码

//创建词典对象,初始化长度为10
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithCapacity:10];

//向词典中动态添加数据
[dictionary setObject:@"雨松MOMO" forKey:@"name"];

[dictionary setObject:@"15810463139" forKey:@"number"];


//通过KEY找到value
NSObject *object = [dictionary objectForKey:@"name"];

if (object != nil) {
NSLog(@"通过KEY找到的value是: %@",object);
}



int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
复制代码

词典类的存在就是为了解决在大量数据中查找方便,因为它是通过key直接找到value所以速度很快,避免一个个的遍历寻找造成的效率低下,善用字典类会帮你的程序提速。

文章出处:http://blog.csdn.net/xys289187120/article/details/682373



NSNull


NSNull大概是Cocoa里最简单的类了,只有一个方法

+ (NSNull *) null;

可以这样添加到集合中

[contact setObject: [NSNull null]

forKey: @"home fax machine"];

访问时:

id homefax;

homefax = [contact objectForKey: @"home fax machine"];

if (homefax == [NSNull null]) {

// ... no fax machine. rats.

}

//[NSNull null]总是返回一样份数值,所以你可以使用“==”讲该值与其他值进行比较……


NSDictionary和NSMutableDictionary

A) NSDictionary

字典是关键字和其定义的集合,也被成为散列表或关联数组,使用的是键查询的优化存储方式

1)创建方法: 使用dictionaryWithObjectsAndKeys:来创建字典

+ (id) dictionaryWithObjectsAndKeys: (id) firstObject, ...;

使用方法:

Tire *t1 = [Tire new];

Tire *t2 = [Tire new];

Tire *t3 = [Tire new];

Tire *t4 = [Tire new];

NSDictionary *tires;

tires = [NSDictionary dictionaryWithObjectsAndKeys:

t1, @"front- left", t2, @"front- right",

t3, @"back- left", t4, @"back- right", nil];

2)常用方法

- (id) objectForKey: (id) aKey;

使用方法:

Tire *tire = [tires objectForKey: @"back- right"];   //如果没有则会返回nil值

B) NSMutableDictionary

1)创建方法:

可以向类NSMutableDictionary发送dictionary消息

也可以使用函数+ (id) dictionaryWithCapacity: (unsigned int) numItems;

2)常用方法

可以使用setObject:forKey:方法给字典添加元素:

- (void) setObject: (id) anObject forKey: (id) aKey;

- (void) removeObjectForKey: (id) aKey;

使用方法:

NSMutableDictionary *tires;

tires = [NSMutableDictionary dictionary];

[tires setObject: t1 forKey: @"front- left"];

[tires setObject: t2 forKey: @"front- right"];

[tires setObject: t3 forKey: @"back- left"];

[tires setObject: t4 forKey: @"back- right"];

//若已经存在,则会用新值替换原有的值

[tires removeObjectForKey: @"back- left"];

23、不要创建NSString、NSArray或NSDictionary的子类,因为在Cocoa中,许多类实际上是以类簇的方式实现的,即他们是一群隐藏在通用接口之下的与实现相关的类

24、Foundation实例 //查找文件

A)使用枚举遍历

int main (int argc, const char *argv[])

{

NSAutoreleasePool *pool;

pool = [[NSAutoreleasePool alloc] init]; //自动释放池

NSFileManager *manager; //Cocoa中有很多类都是单实例构架,即只需要一个实例,你真的只需要一个文件管理器

manager = [NSFileManager defaultManager]; // defaultManager方法创建一个属于我们的NSFileManager对象

NSString *home;

home = [@"~" stringByExpandingTildeInPath]; // stringByExpandingTildeInPath方法可将~替换成当前用户的主目录

NSDirectoryEnumerator *direnum; //NSEnumerator的子类

direnum = [manager enumeratorAtPath: home]; //创建一个枚举条件

NSMutableArray *files;

files = [NSMutableArray arrayWithCapacity: 42]; //把搜索的结果作为文件存储

NSString *filename;

while (filename = [direnum nextObject]) {//调用nextObject时,都会返回该目录中的一个文件的另一个路径,也可搜索子目录

if ([[filename pathExtension]   // pathExtension输出文件的扩展名(去掉了前面的点.)

isEqualTo: @"jpg"]) {

[files addObject: filename];

}

}

NSEnumerator *fileenum;

fileenum = [files objectEnumerator];

while (filename = [fileenum nextObject]) {

NSLog (@"%@", filename);

}

[pool drain];

return (0);

} // main

B)使用快速遍历

int main (int argc, const char * argv[]) {

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

NSFileManager *manager;

manager = [NSFileManager defaultManager];

NSString *home;

home = [@"~" stringByExpandingTildeInPath];

NSMutableArray *files;

files = [NSMutableArray arrayWithCapacity: 42];

for (NSString *filename

in [manager enumeratorAtPath: home]) {

if ([[filename pathExtension]

isEqualTo: @"jpg"]) {

[files addObject: filename];

}

}

for (NSString *filename in files) {

NSLog (@"%@", filename);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值