Updated for Xcode 15
Now that you’ve met arrays, dictionaries, and sets, I want to pause for a moment to give you chance to solve a small coding challenge. It’s not designed to trip you up, but instead to encourage you to stop for a while and think about what you’ve learned.
This time the challenge is to create an array of strings, then write some code that prints the number of items in the array and also the number of unique items in the array.
I’ll provide some hints below, but please take the time to think about a solution before you read them. Trust me: forgetting what you’ve learned then re-learning it actually makes it sink in deeper!
Hacking with Swift+ subscribers can get a complete video solution for this checkpoint here: Solution to Checkpoint 2. If you don’t already subscribe, you can start a free trial today.
Still here? Okay, here are some hints:
let albums = ["Red", "Fearless"]
albums.count
.count
also exists for sets.Set(someArray)
TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.