获取多维数组的键(`[String:[String:String]]`)

时间:2016-06-07 05:22:33

标签: ios arrays swift multidimensional-array

我有一个结构为[String: [String: String]]的多维数组。我能够通过for循环访问[String: String]位,但我无法弄清楚如何访问主键(此位[String: [)。

let items = snapshot.value as! [String: [String: String]]

for item in items.values {
    if let from = item["from"] {
         self.users.append(from)   // this works
    }
}

如何到达此节点的主ID?

节点如下所示:

- table
     - "1"
        - "from": "AA"
        - "to": "BB"
     - "2"
        - "from": "AA"
        - "to": "BB"

我想获得var array = ["1", "2", "3"]

2 个答案:

答案 0 :(得分:3)

使用元组会很清楚试试这个:

$mail = new PHPMailer(true);
$mail->Host = "smtp.office365.com";
$mail->Port       = 587;
$mail->SMTPSecure = '';
$mail->SMTPAuth   = true;
$mail->Username = "email";   
$mail->Password = "password";
$mail->SetFrom('email', 'Name');
$mail->addReplyTo('email', 'Name');
$mail->SMTPDebug  = 2;
$mail->IsHTML(true);
$mail->MsgHTML($message);
$mail->Send();

更新

let dict = ["1":["from":"aa","to":"bb"],"2":["from":"AA","to":"BB"]]

var array = [String]()
for (_ ,value) in dict{
    if let v = value["from"] {
        array.append(v)
    }
}
print(array)

答案 1 :(得分:0)

对于这样的数组,你有key->值对,你可以将键值作为元素.0访问,将值作为元素.1

在操场上试试看它是否过得很快:

for firstLevel in tableArray {
    print(firstLevel.0)
    for innerLevel in firstLevel.1 {
        print(innerLevel.0)
        print(innerLevel.1)
    }
    print("\n")
}