Swift:通过索引进行字典访问 [英] Swift: dictionary access via index
本文介绍了Swift:通过索引进行字典访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用UITableViewCells中的字典中的值。我可以通过使用indexPath.row来获取那些来自字典的那些值,而keyPath.section是key吗?
I want to use values from a dictionary in my UITableViewCells. Can I get those from a dictionary by using indexPath.row for the value and indexPath.section for the key?
推荐答案
你可以获取密钥数组,并得到 indexPath.section
的密钥,如下所示:
you can get the keys array and get the key at indexPath.section
like this:
Array(yourDictionary.keys)[indexPath.section]
,您可以在 indexPath.row
from values数组,如下所示:
and you can get the value at indexPath.row
from the values array like this:
Array(yourDictionary.values)[indexPath.row]
编辑:
如果你想得到一个你应该写的特定部分键的值:
if you want to get the values of a specific section key you should write:
let key = Array(yourDictionary.keys)[indexPath.section]
let array = yourDictionary[key]
let value = array[indexPath.row]
这篇关于Swift:通过索引进行字典访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文