A set stores distinct values of the same type in a collection with no defined ordering. No ordering means it’s just a jumbled mess!
Use a set instead of an array when the order of items is not important, or when you must ensure that an item only appears once.
Create a set:
var letters = Set<Character>()
Insert a member:
Initialise with values at declaration:
var favs: Set<String> = ["Pizza", "Wine", "Wine again"]
Swift can infer the type if all elements are the same type:
var favs: Set = ["Pizza", "Wine", "Wine again"]
Sets can use functions such as count, just like arrays! As you learn to use Swift the differences between arrays and sets will
become more obvious to you (apart from the big one of ordered / vs non-ordered).
Just PLEASE remember that sets have no order. Sets also cannot store identical values, e.g.:
var favs: Set = ["Pizza", "Wine", "Wine again", "Wine"]
This would only contain the element “Wine” once! Someone drank my wine 🙁