ios开发中判断point点是否在一个区域_ios开发中 NSDictionary 基本使用教程

时间:2019-11-20  来源:ios  阅读:

不同于数组,字典(也被称为散列表或关联数组)使用的是键查询的优化存储方式。它可以立即找出要查询的数据,而不需要遍历整个数组进行查找。

1.不可变词典NSDictionary

字典初始化

NSNumber *numObj = [NSNumber numberWithInt:100];

以一个元素初始化

NSDictionary *dic = [NSDictionary dictionaryWithObject:numObj forKey:@"key"];

初始化两个元素

NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:numObj, @"valueKey", numObj2, @"value2",nil];

初始化新字典,新字典包含otherDic

NSDictionary *dic = [NSDictionary dictionaryWithDictionary:otherDic];

以文件内容初始化字典

NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];


常用方法

获取字典数量

NSInteger count = [dic count];

通过key获取对应的value对象

NSObject *valueObj = [dic objectForKey:@"key"];

将字典的key转成枚举对象,用于遍历

NSEnumerator *enumerator = [dic keyEnumerator];

获取所有键的集合

NSArray *keys = [dic allKeys];

获取所有值的集合

NSArray *values = [dic allValues];


2.可变数组NSMutableDictionary

初始化一个空的可变字典

NSMutableDictionary *dic2 = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"v1",@"key1",@"v2",@"key2",nil];

NSDictionary *dic3 = [NSDictionary dictionaryWithObject:@"v3" forKey:@"key3"];

向字典2对象中添加整个字典对象3

[dic2 addEntriesFromDictionary:dic3];

向字典2对象中最佳一个新的key3和value3

[dic2 setValue:@"value3" forKey:@"key3"];

初始化一个空的可变字典

NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];

将空字典1对象内容设置与字典2对象相同

[dic1 setDictionary:dic2];

将字典中key1对应的值删除

[dic1 removeObjectForKey@"key1"];

NSArray *array = [NSArray arrayWithObjects:@"key1", nil];

根据指定的数组(key)移除字典1的内容

[dic2 removeObjectsForKeys:array];

移除字典所有对象

[dic1 removeAllObjects];


遍历字典

快速枚举

for (id key in dic){

id obj = [dic objectForKey:key];

NSLog(@"%@", obj);

}

一般枚举

NSArray *keys = [dic allKeys];

inr length = [keys count];

for (int i = 0; i < length;i++){

id key = [keys objectAtIndex:i];

id obj = [dic objectForKey:key];

NSLog(@"%@", obj);

}

通过枚举类型枚举

NSEnumerator *enumerator = [dic keyEnumerator];

id key = [enumerator nextObject];

while (key) {

id obj = [dic objectForKey:key];

NSLog(@"%@", obj);

key = [enumerator nextObject];

}



NSDictionary等基本类型的使用方法

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"];

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

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);

}

ios开发中判断point点是否在一个区域_ios开发中 NSDictionary 基本使用教程

http://m.bbyears.com/shoujikaifa/80199.html

推荐访问:ios开发中定位 ios开发中textfield中没法输入中文 ios开发中wifi强度 ios开发中报错直接跳到main函数 ios开发中的数据库 ios开发中多线程 ios开发中判断是否是present ios开发中让titleview靠右显示 ios开发中设置行间距
相关阅读 猜你喜欢
本类排行 本类最新