Swift4 Day-05 集合類型—Dictionary

Albert
Albert
Sep 2, 2018 · 2 min read

昨天有點事情,忙到現在來補個!抱歉抱歉

Dictionary

字典儲存的是相戶關聯的同一類型的 key 及同一類型的 value 所組成的集合,所組成的集合,Dictionary 內的值沒有特定的順序。當我們需要找特定標記的值時,我們就可以使用 Dictionary。

創建空 Dictionary

與 Array 和 Set 不同的是,Dictionary 多了一個 key 可以使用, key 和 value 的型別可以是不同的型別。

訪問及修改 Dictionary

這部大家應該已經熟到不能再熟了,如果不小心給他忘記的話,下面有連結可以點進去看看!
回顧 → Swift 4 Day-03 集合類型 — Array

nil 及 updateValue

我們可以用 nil 來設定 value,也能使用 updateValue 來修改 Dictionary 內的值

遍歷字典

一樣,我們可以用 for in 迴圈來去看每個迴圈來去看每個 key 及 value

今天的 Dictionary 就到這裡囉!如果有其他的問題歡迎留言給我!\(*´∀`)/

Albert 的 Swift4 語法集:

Swift4 Day-01 基本語法
Swift4 Day-02 Option 及 nil
Swift4 Day-03 集合類型 — Array
Swift4 Day-04 集合類型—Set

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade