NEW: Learn to build amazing SwiftUI apps for macOS with my new book! >>

Checkpoint 2

Paul Hudson    @twostraws   

Updated for Xcode 13.3

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:

  1. You should start by creating an array of strings, using something like let albums = ["Red", "Fearless"]
  2. You can read the number of items in your array using albums.count.
  3. count also exists for sets.
  4. Sets can be made from arrays using Set(someArray)
  5. Sets never include duplicates.
Hacking with Swift is sponsored by RevenueCat

SPONSORED Spend less time managing in-app purchase infrastructure so you can focus on building your app. RevenueCat gives everything you need to easily implement, manage, and analyze in-app purchases and subscriptions without managing servers or writing backend code.

Get Started

Sponsor Hacking with Swift and reach the world's largest Swift community!

Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

Average rating: 5.0/5

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.