我有一系列 table
的词典let table: [
"0": ["a": "30","b": "21"],
"1": ["a": "31","b": "22"],
"2": ["a": "32","b": "23"],
"3": ["a": "33","b": "24"],
"4": ["a": "34","b": "24"],
]
所以,我想循环遍历每个键(它们将始终是Int类型(通常它显示索引路径))。我可以循环使用这些键吗?
像:
var keys:Int = 0
// Iterate through the dictionary
for (key) in tblData {
//What should i check for here??
}
解决方案: 我能够得到你所有的建议,所以谢谢大家。我做了以下检查,以检查字典键是否是一个Int,是否可用,但我仍然无法打破循环:
let keys = (demo["data"]!["table"]!.keys).sort()
for k in keys{
var num = Int(k)
if num != nil {
print("Yeah! its an Integer")
}
else {
print("uh-oh! not an Integer")
}
}
但我仍然无法打破循环。我应该在哪里使用休息和应该是什么条件。任何人都可以建议我吗?
Swift版本:2.3, Xcode版本:7.3
答案 0 :(得分:2)
试试这个:
var fileNamelist = new List<string>();
foreach (DataGridViewRow dtrow2 in frm.dataGridView2.Rows)
{
con = new SqlConnection(cs.DBConn);
con.Open();
//if null or empty set it to a default value. here i set it to "".
var fileName = string.IsNullOrEmpty(dtrow2.Cells[2].Value.ToString())
? ""
: dtrow2.Cells[2].Value.ToString();
string querySelect = "SELECT FileName FROM SentRecycle WHERE FileName ='" + fileName + "'";
cmd = new SqlCommand(querySelect);
frm.ShowDataGridView();
cmd.Connection = con;
reader = cmd.ExecuteReader();
if (reader.Read() == true)
{
MessageBox.Show("This file already in use!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
fileNamelist.add(fileName);
if ((reader != null))
{
reader.Close();
}
//return; this will finish loop. so removed!
}
}
//遍历字典
let dic = [
"data": [
"table": [
0: [
"service_details": "(3 months)",
"bill_date": "2016-10-27"
],
1: [
"service_details": "(3 months)",
"bill_date": "2016-07-26"
],
"2": [
"service_details": "(1 year)",
"bill_date": "2015-12-29"
],
3: [
"service_details": "Installation charge",
"bill_date": "2015-07-27"
],
"4": [
"service_details": "(1 year)",
"bill_date": "2015-07-27"
]
]
]
]
}
<强>输出:强>
关键是 - &gt; 3
服务明细 - &gt;安装费
帐单日期 - &gt; 2015年7月27日
关键是 - &gt; 0
服务明细 - &gt; (3个月)
帐单日期 - &gt; 2016年10月27日
关键是 - &gt; 1
服务明细 - &gt; (3个月)
帐单日期 - &gt; 2016年7月26日
答案 1 :(得分:0)
如果tblData
是字典,那么您可以使用以下方法遍历所有键:
for key in dic.keys {
print(dic[key])
}
答案 2 :(得分:0)
你可以这样做
if let myTestKonstant = myDictionary[Int(store.itemID)]{
// successful assignment code here
}
// where I'm hoping a failure to assign falls through to...