iOS进阶指南#iOS#HeminWon

字典(NSDictionary 与NSMutableDictio

2016-07-20  本文已影响905人  nalis风

NSDictionary 保存具有映射关系的数据,一组key 另一组 value Map的key不可重复

key和value都可以是任何引用类型的数据

key和value存在单项一对一  指定key就能找到唯一的,确定的value

allKeys 方法 返回所有key组成集合 返回值为NSArray类型

7.8.1NSDictionary的功能和用法

以dictionary开始的类方法和以init开头的实例方法

1.dictionary: 创建空字典

2、dictionaryWithContentsOfFile:、initWithContentsOfFile:读取指定文件内容,使用指定文件初始化 通常为dictionary自己输出形成

3.dictinaryWithDictionary:/initWithDictionary:使用已有的NSDictionary

4.dictionaryWithObject:forKey:使用单个key_value

5.dictionaryWithObjects:forKeys:/initWithObjects:forKeys:使用两个NSArray分别制定key、value集合,以达到创建多组的目的

6.dictionary:withOBjectsAndKeys:/initWithObjectsAndKeys:需要按照 value1,key1,value2,key2,...,nil的格式传入

访问字典:

1.count: 所包含的KV对个数

2.allKeys

3.allKeysForObject:指定value对应的全部key

4.allValues:NSDictionary包含的全部value

5、objectForKey:指定key对应value

6、objectForKeyedSubscript:通过该方法支持,允许通过下表发获取指定K对应V

7.keyEnumerator:key遍历

8.objectEnumerator:value遍历

9.enumerateKeysAndObjectsUsingBlock:迭代

10.writeToFile:atomically:写入指定文件

NSDictionary* dict=[NSDictionary

dictionaryWithObjectsAndKeys:

@"希罗多德",@"xi",

@"亚里士多德",@"ya",

@"苏格拉底",@"su",nil];

[dict print];

NSLog(@"ditc包含%ld个key-value对",[dict count]);

NSLog(@"dict的所有Key为:%@",

[dict allKeys]);

NSLog(@"dict的所有 value为:%@",

[dict allValues]);

// 遍历

//枚举器

NSObject* value;

NSEnumerator * en=[dict objectEnumerator];

while (value=[en nextObject]) {

NSLog(@"%@",value);

}

//方法

[dict enumerateKeysAndObjectsUsingBlock:^(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {

NSLog(@"%@",obj);

}];

7.8.2对NSDictionary 的key排序

对key的排序,返回的是所有key组成的NSArray:

1、keysSortedByValueUsingSelector:根据NSictionary的所有value的指定方法返回值对Key排序:调用value的该方法必须返回NSOrderedAscending/NSOreredDescending/NOreredSame三个之一

2、keysSortedByValueUsingComparator:该方法使用指定的代码块遍历KV对,返回三个值之一

3.keysSortedByValueWithOptions:usingComparator:可传入额外的NSEnumerationOptions

NSOrderedAscending/递增

NSOreredDescending/递减

NOreredSame

value对应的字符串越长,value对应的字越大

NSDictionary* dict=[NSDictionary

dictionaryWithObjectsAndKeys:

@"希罗多德",@"xi",

@"亚里士多德",@"ya",

@"苏格拉底",@"su",nil];

[dict print];

NSArray* arr1=[dict keysSortedByValueUsingSelector:

@selector(compare:)];

NSArray* arr2=[dict keysSortedByValueUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {

if ([obj1 length]  >[obj2 length]) {

return  NSOrderedDescending;

}else if ([obj1 length]<[obj2 length]){

return NSOrderedAscending;

}else{

return NSOrderedSame;

}

//            [arr2 writeToFile:@"myFile.txt" atomically:YES];

//            NSArray* arr3=[arr3]

NSLog(@"%@",arr2);

7.8.3 对NSDictionary 的key进行过滤

所有key执行过滤, 返回NSSet

keysOfEntriesPassingTest:使用代码块迭代处理NSDictionary的每个key-value对。代码块必须返回BOOL类型的值,只有单改代码返回YES时,该key才会被保留下来,可以接受三个参数。

第一个参数:正在迭代处理的key

第二个参数:迭代处理的value

第三个参数:还需要继续迭代 如果将第三个采纳数设置为NO 该方法会立即停止迭代

keysOfEnteriesWithOptions:passingTest:

额外附加一个NSEnumerationOptions的参数

NSSet* set=[dic keysOfEntriesPassingTest:^BOOL(id  _Nonnull key, id  _Nonnull obj, BOOL * _Nonnull stop) {

return (BOOL)([obj integerValue]>65);

}];

7.8.4 使用自定义类作为NSDictionary的key

必须满足:

1.该自定义类正确重写过isEqual:和hash方法

2.实现了copyWithZone方法,该方法最好能返回对象的不可变副本

建议实现NScopying协议

为了防止key值被修改导致的NSDictionary完整性被破坏,NSDictionary只要程序把任何对象都作为key放入NSDictionary时,NSDictionary总会先调用该Key的copy方法复制对象的不可变副本,然后使用该副本作为NSDictionary的key

-(id)copyWithZone:(NSZone*)zone

{

NSLog(@"——正在复制——");

//复制一个对象

FKUser* newUser=[[self class] allocWithZone:zone] init];

//将被复制对象的实变量的值赋给新对象的实例变量

newUser->name=name;

newUser->pass=pass;

return newUser;

}

#improt

#import "NSDictionary+print.h"

#import "FKUser.h"

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

{

@autoreleasepool{

FKUser* ul=[[FKUser alloc] initWithName;@“bai”

pass;@"345"];

//直接使用多个value,key的形式创建NSDIcitonary对象

NSDicitonary* dict=[NSDictionary

dictionaryWithObjectsAndKeys:

@"one",[FKUser alloc] initWithName:@"sun"

pass:@"123"],

@"two",u1,

@“three”,[[FKUser alloc] initWithName:@"sun"

pass:@"123"],

@“four,[[FKUser alloc] initWithName:@"tang"

pass:@"178"],nil];

];

}

}

7.8.5 NSMutableDicitonary的功能

类似NSMutableArray 与NSArray

1.setObject:forKey:新增或覆盖已有的

2.setObject:forKeyedSubscript: 通过该方法的支持,允许程序通过下标法设置KV对

3.addEntriesFromDictionary:将另一个NSDictionnary中所有的KV对复制到NSDIctionary中

4.setDicitonary: 替换

5.removerObjectForKey:根据key来删除KV对

6.removeAllObjects

7.removeObjectsForKeys:使用多个key组成的NSArray作为参数,同时删除多个Key对应KV对

上一篇 下一篇

猜你喜欢

热点阅读