【学习笔记】【Foundation】字典

字典NSDictionary:

 用于保存具有映射关系的数据:key-value;

创建NSDictionary的常用方法:

dictionary:

dictionaryWithContentsOfFile:/initWithContentsOfFile:

dictionaryWithDictionary:/initWithDictionary:

dictionaryWithObject:forKey:

dictionaryWithObjects:forKeys:/initWithObjects:forKeys:  //使用两个NSArray指定value跟key;

dictionaryWithObjectsAndKeys:/initWithObjectsAndKeys:

简化方式:

@{key1:value1,key2:value2,...};

常用的访问value跟key的方法:

count:

allKeys:

allKeysForObject:

allValues:

objectForKey:

valueForKey:

objectForKeyedSubscript: //通过该方法的支持,允许NSDictionary通过下标法来获取指定key对应的value。。。。所以怎么用

keyEnumerator:

objectEnumerator:

enumeratorKeysAndObjectsUsingBlock:使用指定的代码块来执行该集合中所有的key-value对。

enumeratorKeysAndObjectsWithOptions:usingBlock:

writeToFile:atomically:

对key进行排序:

keySortedByValueUsingSeletor:

keySortedByValueUsingComparator:

keySortedByValueWithOptions:usingComparator:

对key进行过滤:

keysOfEntriesPassingTest:

keysOfEntriesWithOptions:passingTest:

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

必须满足两个条件:

1.正确重写过isEqual和hash方法

2.该自定义类必须实现了copyWithZone方法

 

可变字典 NSMutableDictionary:

setObject:forKey:;

setObject:forKeyedSubscript:

addEntriesFromDictionary:

setDictionary:

removeObjectForKey:

removeAllObjects:

removeObjectsForKeys://使用多个key组成的NSArray作为参数

 

posted on 2015-06-09 13:31  新人喵  阅读(147)  评论(0编辑  收藏  举报