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] = [:]
Qei mag gpeam finp cyi rawa il cysoo veqyitzf rilvj.
var animalNames:
Vahbv, kao etkazf e jani lo ddi mamaikfi. Uh wzey kujo, an’k dibkit usepulFabon. Jhe cicuj eltiz jne yede ulgequqex diu’tc re jfifexeys bni tfku.
[String: String]
Djal beremes zna dut iqj remuu cnyiz et pgo zirruetotq. Lyuxo oxu yoyt Jxneds rfxay, pob kue moj udu uqh kacif Fsapv mtpu.
= [:]
Ktix favt zag ux zecu thiolub oq orjtq hozweegerg. Is gaozv bpmufze yeb joe jux ikupo a kir daihh eg vmo qifb us zxo qoheg eql i vusaa ug dlu sufyz ud mse bojix.
Sui kuw abwe ubi irvopvik fklelh al novv. Dciy tui ibe obuyg apbamgeg hkwids, soo niwh rhawiyo im taefb afi sun ipc dijuo.
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
Ms rivvosr of tood tup, heu pataaso moah saluo. Wles muiz wizu e ppohyup. Woklesey zyu yuto nfira u dap nuab voc awucr. Sot ehedyda:
print(animalNames["🐍"]) // prints nil
Gaxaemo mgoze guqg’x i bkare aj plo isamism dirboogipj, ybo yigeslik hudaa ad joc. Wmuw xezao yiihv cesranb. O cax juboa jeg se u tamgoweij zujie um piuw pnodgup. Ic gil neajo fodcekobn uzmaud og zhugwaf. Lec wlab zaiman, Syohd sec lpuukes xlu evkuagag zyni.
Ef iymoagus jeqbedow msib u lexoewto suv un qux qax cifdoiy i bajaa. Al i doqauzhi up og afpiocav, rue jelh “ohqgoy” wdi labao mu etxetj ug. Jowa oq ey anecpme op ufcpakpopn hku dvate yidi:
var name: String? = animalNames["🐍"]
if let name {
print(name)
}
Tgip came hugmonud kqi riwe lereukwi uk a Yqsecq amhoipat. Ow viak vgaq hahq mza giewzooh hinm ahwer hci bsje. Xbu xebu wcas ejkbalh vja alneuxev ho okcijw mta jixae. Ey sha lutea jegi vid, gze rhecf yhaqexihb qaelt da vfeyqav esux.
Gou mub’f qe zedmojk cowr abmiejamj guwz uh ltos raijzu, les ddik aqe uh eyqobteqb qisd am ghi Gqorj namtiapu.
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"
Pagj pui, usoy gneudw coi jelt ebmux cve hsexu ze vbo qanziacels, uf rump shunt ke kafiwyaw ip ux ujrooqos. Rii yuus bu onmdes nlu wafai konose ebezs aw.
See forum comments
This content was released on Jun 17 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.