key通常为NSString,key是唯一的,键值不可为空nil,如果需要一个空值可以使用NSNull
//初始化一个元素NSDictionary *dic = [NSDictionary dictionaryWithObject:@"value" forKey:@"key"];//初始化多个元素NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"value" ,@"key",@"values2",@"key2",nil];//以字典初始化NSDictionary *dic = [NSDictionary dictionaryWithDictionary:otherDic];;//以文件内容初始化NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:path];
常用方法
//总数NSInteger count = [dic count];//取值NSObject *value = [dic objectForKey:@"key"];//将字典的key转成枚举,用于遍历NSEnumerator *enum = [dic keyEnumerator];//获取key的集合NSArray *keys = [dic allKeys];//获取所有的valueNSArray *values = [dic allValues];