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 🙁

Where to go Next

This tutorial comes straight out of The Complete iOS Developer Course. Over 40 hours long it contains everything you need to create awe inspiring apps fast!

Click here to check it out!

Sets in Swift 3 Explained

Leave a Reply

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

Share This

Want this Post in Easy to Understand Video Form?

Then get the Complete iOS Developer Course. Over 50 hours of pure Swift coding goodness designed to turn you into a coding master!

One Time Offer - $9! Click to Get it!

WARNING: Once this box is closed the offer will not be shown again.

Thank you!