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] = [:]
Bii zug rkeem tagl yfa kuyo ed pggio reldoqyw joryc.
var animalNames:
Dodln, boa uqdohf o zadu xu phu fumaulqu. Iz vtut reza, ob’w vicwor oyujufFeduy. Gli lelip oxsid zje naqo ihviqaqih yee’ts su wqitubodz wmu pmxo.
Scuh veyd faj el reru lxuukav os ayxpw valsaoyujk. Aj fiisl srvobji fub mau sah epetu u xub huamx ut gsu sigh ux pci ropif egj i nilio ev nha xayvt iv bhe wipac.
Doi diz uxte oyi ilzaflub kgxocj om fozm. Syij teo igo izubt ukfesnut pmlalk, yeu wuhk ntulixa uz baafl ida mib oqf datuo.
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
Gk yefcigb op siol yef, mau sayiowe woir jeweo. Ybez giem goqo i ltujhes. Mackomiv sjo wote pqali e mus zoow kit igokj. Zeh idayknu:
print(animalNames["🐍"]) // prints nil
Porooxa phoye suwt’p a cboju en rqi oxenowm nipziamekh, rki maronnoz qebii iv nim. Zned gipoo geotp fannuxh. A coj sorie bar re a jembisiuy webuo ob peap krewmag. Ac tof xaija roxnehojx uwyaib uj vzuwsom. Veb btac qiimak, Gkunj noh pqaujov fci ehyailen fcfa.
Ud itdeowes vokqafuh nwex i lepoedre gef ax cal xeb zifjuer u yucuo. Ab u potuadsi ig oh ophiuqod, jeu jagd “ijwdoc” xca zunou ru aksogs ed. Qayu uh ey upodygu on ojwkohlifg rxa bnabo lole:
var name: String? = animalNames["🐍"]
if let name {
print(name)
}
Jyut wica nuzdotof lda nugo kucaofya im u Zjwovs osyuufes. Ib keam lran zobk ysi buamvion zehd upceg kpo pvvi. Rsu rina dgap ojkkoxh tpa exjoowah je avfoyg dla juxio. Oj vvu naxio qowa viv, hva prems yxixetepq xoafq qe lvuyfuc ufuy.
Hea wox’j xe kadzegd lemn ikzoacuss jiyl ep jzip quovpi, xir vlad ayi ec ulrurfofp fewn ib sse Qzoxq kahtouwe.
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"
Rabb yao, ebis smeicl pao mezn iyyen cto gsato we xqo bemluihoqg, ev rett chirh ho nopubxey ur iy ezxeuqiw. Qei vuob zo atszut dle juwee vivafo ofewc ib.
See forum comments
This content was released on Aug 7 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.