|< When should you use an array, a set, or a tuple in Swift?||Why does Swift have default values for dictionaries? >|
Updated for Xcode 14.0 beta 1
Dictionaries and arrays are both ways of storing lots of data in one variable, but they store them in different ways: dictionaries let us choose a “key” that identifies the item we want to add, whereas arrays just add each item sequentially.
So, rather than trying to remember that array index 7 means a user’s country, we could just write
user["country"] – it’s much more convenient.
Dictionaries don’t store our items using an index, but instead they optimize the way they store items for fast retrieval. So, when we say
user["country"] it will send back the item at that key (or nil) instantly, even if we have a dictionary with 100,000 items inside.
Remember, you can’t be guaranteed that a key in a dictionary exists. This is why reading a value from a dictionary might send back nothing – you might have requested a key that doesn’t exist!
SPONSORED In-app subscriptions are a pain to implement, hard to test, and full of edge cases. RevenueCat makes it straightforward and reliable so you can get back to building your app. Oh, and it's free if your app makes less than $10k/mo.
Link copied to your pasteboard.