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:

Insert a member:

Initialise with values at declaration:

Swift can infer the type if all elements are the same type:

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.:

This would only contain the element “Wine” once! Someone drank my wine 🙁

Sets in Swift Programming Explained
Tagged on:             

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share This

Exhausted by your 9 - 5?

Would you prefer to work for half the time and earn double? The first step to your freedom is understanding how apps make money. Simply enter your email below and I will send you a free PDF describing the process.

If you're serious about improving your life then do not miss this opportunity.


You have subscribed! Please check your email to confirm this subscription and to receive your item!