#import <Foundation/Foundation.h>
int main(int argc,const char * argv[])
{
@autoreleasepool {
//是一系列 “键——值”的集合
//创建字典:dictionaryWithObjectsAndkeys:
NSDictionary *glossary = [NSDictionarydictionaryWithObjectsAndKeys:[NSNumbernumberWithInteger:97],@"nicky",
[NSNumbernumberWithInteger:95],@"johnson",
[NSNumbernumberWithInteger:90],@"black",
[NSNumbernumberWithInteger:100],@"lily",nil];//常量字典不可变
NSLog(@"%@",glossary);
//查找到对象:objectForKey:参数s关键字,返回的是值。
NSLog(@"%@",[glossaryobjectForKey:@"johnson"]);//键查值
///可变字典NSMutableDictionary///
//创建新的可变字典:dictionaryWithCapacity:
//给字典添加元素:setObject:forKey:
//如关键字已存在则替换原有的值
//删除指定d元素:removeObjectForKey;
NSMutableDictionary *mGlossary = [NSMutableDictionarydictionary];
[mGlossary setObject:[NSNumbernumberWithInteger:97]forKey:@"nicky"];
[mGlossary setObject:[NSNumbernumberWithInteger:95]forKey:@"johnson"];
[mGlossary setObject:[NSNumbernumberWithInteger:90]forKey:@"black"];
[mGlossary setObject:[NSNumbernumberWithInteger:100]forKey:@"lily"];
[mGlossaryremoveObjectForKey:@"johnson"];
NSMutableDictionary *my=[NSMutableDictionarydictionaryWithCapacity:20];//预估字典个数
NSLog(@"----------------------");
//对字典请求枚举器///
//遍历字典中的所有对象,取的是值
NSEnumerator *enumerator;
enumerator = [glossaryobjectEnumerator];
id obj;
while (obj = [enumerator nextObject]) {
NSLog(@"I found %@",obj);
}
//遍历字典中的所有关键字
NSEnumerator *enumerator1;
enumerator1 = [glossarykeyEnumerator];
id obj1;
while (obj1 = [enumerator1 nextObject]) {
NSLog(@"I found %@",obj1);
}
//快速枚举
//对字典中进行快速枚举会遍历数组中的每个关键字
for(id key in glossary)
{
NSLog(@"I found %@",key);
}
}
return 0;
}