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

Get your FREEapp monetisation flowchart!

Use our epic flowchart to help you find the money in your idea! If you're thinking of making an app then this is vital information!


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