Swift學習 8 字典

2021-07-02 13:19:36 字數 1479 閱讀 7353

字典是一種儲存多個相同型別的值的容器。每個值(value)都關聯唯一的鍵(key),鍵作為字典中的這個值資料的識別符號。和陣列中的資料項不同,字典中的資料項並沒有具體順序。

var games: [string:string] = [「diablo3」:」2014:8:12」,

「dragon age」:」2014:10:07」]

var games = [「diablo3」:」2014:8:12」,「dragonage」:」2014:10:07」]

games[「littlebigplanet3」] = 「2014:11:29"

games[「littlebigplanet3」] = 「2014:11:30"

var nameofintegers = [int:string]()

namesofintegers[16] = 「sixteen"

namesofintegers = [:]

if let oldvalue = games.updatevalue(「2014:8:14」,forkey:」diablo3」)

if let releasedate = games[「diablo3」]else

games[「littlebigplanet3」] = nil 移除鍵值

games.removevalueforkey(「diablo3」) 和updatevalue一樣

let airports = [「tyo」:」tokyo」,」lhr」:」london」]

for (airportcode, airportname) in airports

for airportcode in airports.keys{}

for airportname in airports.values{}

varairports:

dictionary

<

string

,string

> = ["tko":"tokyo","cha":"china"]

println("the airports dictionary has \(airports.count) airport")

airports["lon"] = "london"

airports["lon"] = "london weather"

airports["cha"] = nil

iflet oldvalue = airports.updatevalue("dublin", forkey: "cha")else

for (airportnumber,airportname) in airports

for key in airports.keys

for value in airports.values

let airportcode = array(airports.keys)

var nameofintergers = dictionary()

nameofintergers = [:]

Swift學習記錄 8 字典的基本操作

字典和陣列的操作大致相同 只是在合併方面略有出入 1.定義字典 定義方法非常多 只寫幾種常用寫法 1.1 定義不可變字典 let dic name my age 18,height 1.85 1.2 定義可變字典 建立乙個值為任意型別的字典 var dicm string anyobject nso...

Swift學習筆記 6 字典

1.定義 1.基本定義 key 1 value 1,key 2 value 2,key 3 value 3 var dict name xiaoqin female age 20 for key,value in dict 2.型別強制定義 dictionaryvar dict2 dictionar...

Swift學習筆記(9) 字典

字典是一種儲存多個相同型別的值的容器。每個值 value 都關聯唯一的鍵 key 鍵作為字典中的這個值資料的識別符號。字典中的資料項並沒有具體順序。通過識別符號 鍵 訪問資料。使用dictionary key,value 定義 key 字典中鍵的資料型別。value 字典中對應於這些鍵所儲存值的資料...