WWDC24 SALE: Save 50% on all my Swift books and bundles! >>

Checkpoint 5 one-line solution feedback

Forums > 100 Days of SwiftUI

Hi all -

After reading the requirements for checkpoint 5, specifically the requirement to call each function one after the other and not use any temporary variables, I came up with this one-line solution (two lines if you count the input provided by the checkpoint):

let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]

for item in (luckyNumbers.filter { $0%2 != 0 }.sorted().map { "\($0) is a lucky number" }) { print(item) }

Most of the other solutions I have found online are quite a bit longer so I wanted to get some feedback on mine.

Really enjoying the course and learning Swift!


One of the objectives was to discover and chain together several array functions.

You found most of them, and chained them together nicely.

WIthout giving you the solution, there's another array function you can use to replace the for item in construct.

Can you find it?

See-> Apple Array Documentation


Ah, yes I think so -

luckyNumbers.filter { $0%2 != 0 }.sorted().map { "\($0) is a lucky number" }.forEach { print($0) }

Thank you for the suggestion!


That's the one!

Three lessons for you.

First, the documentation for array has many, many array functions and convenience variables. You'll find most of Apple's APIs are rich with options.

As some point you'll find yourself asking, "How should I code three separate text fields into a birthdate for my application??" This is where you need to train yourself to stop and say: "I can't be the first developer to have this problem!" Then instead of coding it yourself, take a look into the documentation. Somewhere you'll find a method to create a date from text objects.

Second, every time you ran a method on an array, Swift returns another array. Then you can chain another method to the new array. And do it again. And again.

Some of the chained methods required you to provide extra code, called closures. This sort of functional programming makes Swift very expressive.

Is your code Swifty?

I'll add one addtional comment regarding syntax and style. Swift borrows from best practices of several modern languages. And in many places it's blazed new trails into readability and easy syntax.

In this spirit consider the modulus operator for a moment.

Modulus returns the remainder of simple division of two numbers. This is a maths concept and exists in math libraries for most all languages ever. Even spreadsheets and databases support some form of the modulus operator.

But is the syntax straight forward and readable?

In spreadsheets you might use a function named mod(x,y). In Swift you use the percent symbol, $0%2. Then you have to know that if dividing by 2 returns zero, the number is even! But you have to negate that because you're looking for ODD numbers.

In short there is a lot of potential for error. Error in negating, error in divisors, error in interpretation.

So ask yourself, is there a Swiftier way to do this? That is, is there a way to make this syntax easier to write, easier to read, and easier to interpret? In many articles you might find the writer using the phrase, "This isn't very Swifty."

So this is a long-winded bit of advice. But the Swiftier way to write this is to use an Integer function isMultiple(of:)

if myNumber.isMultiple(of: 2) {
  // this is an EVEN number, because it's a multiple of 2.

Keep Coding


Save 50% in my WWDC sale.

SAVE 50% To celebrate WWDC24, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

Unknown user

You are not logged in

Log in or create account

Link copied to your pasteboard.