How to Create a Set in Swift
Use a Set in Swift when you need a collection of unique values with fast membership checks. Sets are perfect for selected tags, visited IDs, permissions, and duplicate prevention.
What you’ll build or solve
You’ll learn how to create a Set in Swift with literal syntax, explicit typing, empty sets, and common real-world use cases. You’ll also know when arrays are a better fit.
Learn Swift on Mimo
When this approach works best
This approach is the right choice when uniqueness matters more than order.
Common real-world scenarios include:
- Selected filters
- Visited lesson IDs
- Permission names
- Unique email lists
- Tag systems
This is a bad idea when the exact order of values matters for display.
Prerequisites
You only need:
- Basic Swift collections
- Familiarity with arrays
- Understanding of type inference
Step-by-step instructions
Step 1: Create a set with literal syntax
The cleanest syntax uses Set() around an array literal.
Swift
let tags: Set<String> = ["swift", "ios", "swiftui"]
The duplicate "swift" value is automatically removed.
This guarantees uniqueness.
Step 2: Let Swift infer the type
If the context is clear, inference works.
Swift
let permissions: Set = ["read", "write"]
Swift infers Set<String>.
Step 3: Create an empty set
Empty sets need explicit typing.
Swift
var visitedIDs: Set<Int> = []
This is common for selection and tracking state.
Step 4: Use set membership checks
Fast membership is one of the biggest wins.
Swift
if visitedIDs.contains(42) {
print("Already opened")
}
This is faster and clearer than array scanning.
What to look for:
- Sets guarantee uniqueness
- Great for membership checks
- Empty sets need explicit types
- Order should not be relied on
- Arrays are better for ordered UI
Examples you can copy
Unique tags
Swift
let tags: Set<String> = ["swift", "ui"]
Selected IDs
Swift
var selected: Set<Int> = []
Permissions
Swift
let permissions: Set = ["admin", "editor"]
Common mistakes and how to fix them
Mistake 1: Expecting stable order
What the reader might do:
Render a UI list directly from a set.
Why it breaks: set ordering is not guaranteed.
Corrected approach:
Convert to a sorted array.
Mistake 2: Using arrays for uniqueness checks
What the reader might do:
Use [String] for selected tags.
Why it breaks: duplicates can slip in.
Corrected approach:
Use Set<String>.
Mistake 3: Creating empty sets without a type
What the reader might do:
Swift
let ids = []
Why it breaks: Swift cannot infer the element type.
Corrected approach:
Use Set<Type>.
Troubleshooting
If ordering matters, convert the set into an array.
If type inference fails, declare Set<Element>.
If duplicates still appear in UI, inspect the conversion step.
If membership checks are slow, switch from arrays to sets.
Quick recap
- Use sets for unique collections
- Great for fast membership checks
- Empty sets need explicit types
- Do not rely on order
- Convert to arrays for UI display
Join 35M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot