C# 访问嵌套字典中的单个元素
我有一个iOS应用程序,使用嵌套字典向表视图提供数据。词典的结构如下:C# 访问嵌套字典中的单个元素,c#,ios,dictionary,xamarin,C#,Ios,Dictionary,Xamarin,我有一个iOS应用程序,使用嵌套字典向表视图提供数据。词典的结构如下: Dictionary<string1, Dictionary<string2, string3>> dict; (I have named the strings here for clarification purposes) 感谢您的帮助我在访问嵌套元素时遇到困难,但最终还是让它工作了。我只是对我需要如何处理这个问题有一个误解(更不用说它会变得有点毛茸茸的) 我的解决方案: cell.TextL
Dictionary<string1, Dictionary<string2, string3>> dict;
(I have named the strings here for clarification purposes)
感谢您的帮助我在访问嵌套元素时遇到困难,但最终还是让它工作了。我只是对我需要如何处理这个问题有一个误解(更不用说它会变得有点毛茸茸的) 我的解决方案:
cell.TextLabel.Text = dict[dict.Keys.ElementAt(indexPath.Section)].Keys.ElementAt(indexPath.Row); //string2
cell.DetailTextLabel.Text = dict[dict.Keys.ElementAt(indexPath.Section)][dict[dict.Keys.ElementAt(indexPath.Section)].Keys.ElementAt(indexPath.Row)]; //string3
“不对”是什么意思?@AMomchilov我的意思是,我目前访问字典
string2
的方法不对/不可编译,这不会告诉我任何我不知道的事情。这是编译器错误吗?如果是,错误信息是什么?这是运行时错误吗?如果是,堆栈跟踪是什么?这是一个逻辑错误吗?如果是,实际输出和预期输出是什么?KeyNotFoundException
在调试器中输入断点,然后查看字典的内容。查看它是否包含所需的键/值对。还要检查你要用的钥匙是否有效。你真的应该试着清理一下。它很毛茸茸的。
cell.TextLabel.Text = dict[dict.Keys.ElementAt(indexPath.Section)].Keys.ElementAt(indexPath.Row); //string2
cell.DetailTextLabel.Text = dict[dict.Keys.ElementAt(indexPath.Section)][dict[dict.Keys.ElementAt(indexPath.Section)].Keys.ElementAt(indexPath.Row)]; //string3