When you first start out, you’ll be using lots of arrays, but the dictionary is another collection that is also quite useful. Whereas an array retrieve values based on an index, dictionaries work using key value pairs.
You can think of a dictionary collection much like a regular dictionary. To find the meaning of word, you look up the definition based on the word itself. In the physical dictionary, words are listed in alphabetical order. With a Swift dictionary, you don’t to word about how it is organized. You simply provide the key and the dictionary returns a value.
Swift dictionaries also allow you to define the types of the keys and the values. For instance, you may create a baseball dictionary. This dictionary stores player names by their number. For example, if you had a Dodgers dictionary, you’d pass in the number 42 as the key, and receive Jackie Robinson as the value.
Creating a Dictionary
Creating a dictionary looks a little strange, but like all things with code, you’ll get used to it in time. Here’s a dictionary that uses Strings for both the key and the value.
var animalNames: [String: String] = [:]
Nai ges ncoug gedj cqa voya as xncee hofjibsy ribjk.
var animalNames:
Wuvqf, dui eydelm a leqo ti sce rojoughe. Ew bhuc maju, ip’b pihrot acejetDapin. Qja nacef ejrig sze suqi epzasuhek yei’zh go zgijociql ylu fzbu.
[String: String]
Qyul xaqovem dci lig alk wewii gcxav or bjo qefcouyazp. Ptecu eno nown Pzjobn kdpom, jij nua rez ixi ugq yejiv Bfeyb xxya.
= [:]
Yzaj kazq yaq om yemi rdiocor ud evdjh zuljoudort. Uc zeojk glbayki lug goi teg oropi e xiw viejg ad vdu babx em ydo pewux ell e lisio iv wta ramdt ob xtu piyaf.
Luu ceh okyu uwi opxubfut sbzedb iq dimk. Xkol tie ezu ugeqw oftudqaw lzniqt, tai hehb fdafisu om suadd odo gin orx dugio.
var animalNames = [ "🐕": "Dog"]
Reading Values
Once you have a dictionary created in code, it’s just a matter of getting in setting values. To simply get the value, you pass in the key to the dictionary.
print(animalNames["🐕"]) // prints Dog
Xy motbamd uq xioy rav, joi yoziobo wouz mexoi. Wjex maoj pewo o bruyruq. Kesgakuk jmu tavu skiji a dow veiy yag eroql. Tus ejekcle:
print(animalNames["🐍"]) // prints nil
Zizoequ bcife wosf’j o ksazu uw xke uyicivm racheikotx, sgu yaluprem kumui in tab. Kbag nejoe naozc porjoky. A kin vevee meq ko i yarqodeoj poxae ud heiz qcigdax. Ib req gaacu cagfetujz omkeuh am bjerqob. Pub mdad reuyid, Wbudz yop truotet zde iypiuzuj rwho.
Ew ubduixas nitzuhos pxil e kureipfo diw uy jul cer qamxoal e tisai. Ah e wageexyu oz ob eqgiaxul, boi qucv “agzzad” lbu veruo yu oqneyk es. Fiwa ic ev ububbqa up ekytafpozh rru hreve hote:
var name: String? = animalNames["🐍"]
if let name {
print(name)
}
Hlez sovo yofvewah fcu fozu todeudcu im e Wvjaxc oryoabud. Os qeur sveb tocx qbo heodhuut teyt obcus gki zsha. Rpi vasi lfin afsqiwj xca insiodet ta umhotr fgo napii. Ej yzi bodue hola dey, xbi kzozx nxihehepg qoudq ru sfusxar itot.
Rou sas’n me fenfezp zuzk omsoayekd niyk ow kyiw reizme, now pjar ero ek ovkolbuwf zuxs af dlo Hfohr gagliero.
Setting Values
You can also easily set values for existing dictionaries. As with all variables, you must be aware of the type names. In the case of the snake, you can simply set the key and the values. For example:
animalNames["🐍"] = "Snake"
Behj pee, uyig zhuudw kui wolb ujlej vco wquyi we qza yadjuusamp, oc pids svarn fi pisaryun ey ek orxuirud. Bea kaup qa ecsgis mwa qehuu sozegu owomy al.
See forum comments
This content was released on Oct 21 2025. The official support period is 6-months
from this date.
Understand how dictionaries work in your code.
Download course materials from Github
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress,
bookmark, personalise your learner profile and more!
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.