Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Beginning Swift

You're reading from   Beginning Swift Master the fundamentals of programming in Swift 4

Arrow left icon
Product type Paperback
Published in May 2018
Publisher Packt
ISBN-13 9781789534313
Length 202 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Authors (2):
Arrow left icon
Rob Kerr Rob Kerr
Author Profile Icon Rob Kerr
Rob Kerr
Kåre Morstøl Kåre Morstøl
Author Profile Icon Kåre Morstøl
Kåre Morstøl
Arrow right icon
View More author details
Toc

Dictionaries


A Dictionary is an unordered collection of mappings/associations from keys to values. It is very similar to a Set and has the same performance, but stores a key/value pair, and only the key has to be Hashable. It can be used for storing preferences, or when you have a group of named values that are either too many or change too often to be hardcoded. Then, you can use the names as keys.

The full name is Dictionary<Key, Value>, but it is more commonly written as [Key: Value].

Dictionary ignores the order in which values are added or removed, and may change it arbitrarily, just like Set.

Working with Dictionaries

Here are a few basic operations with dictionaries:

  1. Create a dictionary and print it to observe that the order is not preserved:

           var numbers = [0: "zero", 1: "one", 10: "ten", 100: "one hundred"]
           print(numbers) // [100: "one hundred", 10: "ten", 0: "zero", 1: "one"]
  2. Add or change a value:

           numbers[20]...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image