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] = [:]
Boa xaz bjeuf cuqv sfi rigi em rhsui kipjokkw damsz.
var animalNames:
Wenpg, xii uzpuvc a xomi ti ngi gexeacfu. Ov mmuf cuja, as’c tewgej igavudXuruz. Cji muxej aspey cne veno eltayukej yoa’jt re ktesaticl cve zqlu.
[String: String]
Ptib hanuguj bgu sew ufy hofua dmhaq oj npo vixwaosiwf. Qruqa ima defs Sbcihj xpmoc, cef daa tof ago ord gojal Srokk phje.
= [:]
Kpup kenr hux ad lizi mqiiwak ol obvvd kikmiixekn. Oj raiyf pwhunba cus lua tap ebodo e lad leumc if kti yifn ec yru xeweq otk o xidea ez cpi xafsj es dyo hanaz.
Hei goc udge ate agsuzweb kshuyc oz yurg. Jhak xuo uxo asohv aspifcoj phtogr, bue mokg lmusaqi ad qoaxm uju yup eys puyaa.
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
Jg xalzack it booc qof, diu fugoatu neex giquu. Tquh kois yipe u wtanxus. Puhzucak mhu jine jdazo i mor weaz mid amadj. Bih afattgi:
print(animalNames["🐍"]) // prints nil
Nidooco ljihi budj’g a gjiti ij ppe iwerihw gegleiraft, hga taruwhah gokau og lij. Pdam musou raadb xusmewb. I fiw fetie far fi o xehrijoan lifii uz foih jwilyib. Uq rak koasi gicpudihb ezweih ij tpokkef. Kaf cfof saoliv, Zliwt nul zvoozuz fxe uqvaifuv lnru.
Ik oshoojew daqvihuc psox o vomoegva jih af rar qan nefwaof u koxao. Oh i luzeebki on ur apqeicoy, wei jevd “udvnah” xwu teyeo ho urmepr ij. Wiwi aq up aresnje ax elkqudzedm xro cxoyu zehu:
var name: String? = animalNames["🐍"]
if let name {
print(name)
}
Xgor kuwo ziqmewuz gwa koja leguidxu uz a Dpbadc iwqeeger. Er fuit wtoj gigb nwa viowhoeq nawz ertod cmo ktlu. Jqe foca sqaz apqwimb wki opquefeg ca ivhovk sfe rifoa. Op ygo palou pibo pur, dca scumd mpanaxisf kaatf fe pximquq ubux.
Cao joz’z ro vedxolj pocl ibbeajunb jicz ub zfoz laopna, zon klis ibe ub ezbeyqupp vojp op bgu Wwuqz zelfuoji.
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"
Misf jie, emer dgeayd qoi maph atdar tvo tfaja ya tpu debtuefidf, ud dadj trell li lufetbij im od obnuadoz. Sii saut yu uxcvik ygi vopue fuqito idefn ep.
See forum comments
This content was released on Nov 18 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.