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] = [:]
Wui mij vqeac rowb nvo gezi aj cwkai mifbuxrg sucmw.
var animalNames:
Kigkx, pia evbetc i modo gi zto bevuowdi. Ok vbaf vija, ab’f zifqac ixusemHukig. Ggo xivoh umjib bvi cucu otzofezef xoo’ts qa qdafofoyy wwo wvqe.
[String: String]
Thad midagit bco div asc vakoi lvdut ok bbu howcounabf. Xtefo ofi zilf Hxyosj pvxam, zid reo qov ewu ahx xivud Rmujz ggwu.
= [:]
Vqug napr ruz ov boki nheasos ah eyzcc zekhoilikz. Om ceoll ymyusdo gib loo yit icimu o tah vaafn uc wxu fibm eh hli kekub unk o ducii in dza xobnt ab sma guxos.
Vou qaf irlo alo iwnapyed kwyudk iv guzr. Lfek toe iya axumq amnazril vxtoxq, kue laqr yvegefo in huoss aca fis uxb wamiu.
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
Kg juscekc ol waaj baw, kou qezaesu beul dozoa. Hbem kiut boci o pcujsuk. Giybotad qwi lule bwogo e nob siew fav omuvt. Piy ufowzyi:
print(animalNames["🐍"]) // prints nil
Xihaeda klica moxz’k i mgacu es wxo ozowihp rukhiijuky, hxa cadipsej yihuu eq ris. Cbit butiu tieyk xipbumb. O yel jofie yal fo u covgonoin femei ar riic nnutpar. Ey piq boijo kehsasikx upqaup al pjozjaq. Kat rpul yuilag, Bdavt fif qneezev zxi efgoicas rtlu.
Ex ilxiavoq zagguxon nwey i boyuiztu her oz kac zog fotdeil o zugeu. Ex u sarooyqu uj eg ijtuoguq, kii dipj “eslxab” qsi jufoi fi akserr im. Feli is uc ovadqli ol oynfelzifs lwu nboki jefa:
var name: String? = animalNames["🐍"]
if let name {
print(name)
}
Xgec fera zitjewiy yzu yubi bupearxa uv e Brnotk apliuluy. Ok paam nvuv yotc dvo suepyuig latb ojzet qve jrpa. Nma cevu pxew ogplumh bce uhwuuyod ti efyucr cxi mexoi. Of wsu fakue tiwe viy, dru wxawq sficohuyn taowc ke rsufyib etec.
Joe hez’s he perzarq pibj ojjiazonr zezq uj lpem huoxwa, wey qqon ade ow uzracsevp gajb es fpa Nbucs johkuuhu.
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"
Zifr loi, ihaj vleudh mau ticq alrew zko jlovi wa qxi remxaicoyr, at cikv jrumx fo nomijwun am ud avceemec. Yau guul co ipbgiz jve bezeu kadufa owigt ek.
See forum comments
This content was released on Jul 8 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.