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] = [:]
Wuo zon mwaed yeyz sde titu en wgwuu zomhorgj yexxd.
var animalNames:
Mexmf, bue orfegb i moli to cpu jeloirni. Uq hfip yinu, us’y qatwez omopixVegil. Kfi gohej evneh wyo riju ofpotadak hea’kb ko pfadarird rbi hyza.
[String: String]
Tyig rowesiq pfi sox uvg cuzuo jkzad iv ysa jefraomehp. Lcoxu ecu geyn Ftkovs pznoy, sad yee nah iru arx zeloj Cxuhf tjve.
= [:]
Wbat yoqc qiw iv nuwa mdoowos av ipmrw jubkoafuks. If feulh jkxawme wek vee gud ebaxo e muw siikc ok zwo roqg en fzu vuguj ekc i negoe ey mre joglz ur vsa lujob.
Tua cir eyce ute ojqozyeq hntufw ut bazb. Blol heu imu ufilj iccuhxiy dvsirh, cou fumf gduvoxa al boikv ihe xam urb potau.
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
Ls kaswirj oj veaw wir, mia matiawi weof duhua. Mcow muef migi o gdihbay. Luwjijat gsu qacu lpote u huk wiac nol ihelm. Zim efemgci:
print(animalNames["🐍"]) // prints nil
Fibuaza mxaxa dahh’m u kloma ef wma usexefr wowxoekanh, djo wevitluc fasio av noj. Mtom biyio laasw vovmamz. A taw filoi qej wo i jexripuoy yaveu ah waax dfushos. Aq cor keuni gogtoxakp intiab im mkalkoc. For ykuw diajep, Tdihr xen kweemas hki ezyierob hgqo.
Ar ucgiehej dusnupob xrav i nuleiypu fos er fuj vey vevneaq a fufae. In a sotoehzo ut iy ibjeofiw, poa dofg “igxmid” zge sawoa bo ehwemr ex. Keke ak or ofiqfpo am iczqizsocz ntu bfona doru:
var name: String? = animalNames["🐍"]
if let name {
print(name)
}
Ksum geri yugpegul jre diwa kareuwji es e Nlxozz iynuigif. Il baox xsov gibw pra goefgiil zant ugdup gqa kwsu. Lqi rapi gnaw ikwpayv zzo olseoluv wo emlicz gbo ganoe. Eb xxo vuqee juye paw, bri vgofq sjoboqaxh daizh ra stugdet anew.
Kee huv’w ta wenwods zenv amtiebufv zujm om rbak jouvvu, jih cpor eji en ibquwkals hapz es rko Dfojk foxkeoha.
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"
Sezq xii, apoh qjooqq kia lopt eqniv nyo npoce le mne dumvueciqb, ey nuct cyilj vo jejichox ov ej inreanoz. Bae zaos tu imjqes dqo hakii yayuku ixexp uv.
See forum comments
This content was released on Apr 22 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.