2014/06/08

Swift for in走訪字典(Dictionary)

再原廠的教科書上看到了這篇,覺得蠻有趣的




//
//  main.swift
//  test
//
//  Created by CYFang on 6/6/14.
//  Copyright (c) 2014 CYFang. All rights reserved.
//

let m_Dictionary = [
    "A" : [1, 2, 3, 4, 5, 6, 7, 8, 9],
    "B" : [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
    "C" : [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]]

//透過key and values走訪
for (name, numbers) in m_Dictionary{
    println(name + ":\(numbers)")
    for number in numbers{
        print("\(number) ")
    }
    println()
}

//有點類似Python的tupleXD
for index in m_Dictionary{
    println(index)
}



執行結果: