Swift·Swift from optionals to SwiftUI

Collections

Use arrays, dictionaries and sets with map, filter and reduce.

Beginner 12 min read Lesson 3 of 300 Challenge included
What you will be able to do
  • Use arrays for ordered data.
  • Use dictionaries for keyed lookups.
  • Use sets for membership.
  • Prefer map and filter over manual loops.

In plain English

Arrays keep order, dictionaries map keys to values and sets hold unique values. Value semantics mean assignment copies the collection. Use map, filter, reduce and sorted for transformations.

Collections.swiftSwift
let lessons = ["HTML", "CSS", "SQL"]
let long = lessons.filter { $0.count >= 3 }
let lengths = lessons.map(\.count)
let total = lengths.reduce(0, +)
print(long, total)
Your turn

Filter a collection

Easy

Return only lessons whose name has at least three characters.

SWIFT · starter
lessons
Check your understanding before moving on.
Knowledge checkQuestion 1

Which collection stores unique values without order?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Structs and classes
Lesson 3 of 300 · 0% of course