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

SOLVED: Feedback on checkpoint 5?

Forums > 100 Days of SwiftUI

After my first attempt at checkpoint 5, it looked like I had solved it by just using a sort of procedural approach.

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

let oddNumbers = luckyNumbers.filter { $0 % 2 != 0 }

let sortedOddNumbers = oddNumbers.sorted()

let sortedOddNumbersAndMessage = { String($0) + " is a lucky number" }.joined(separator: "\n")

But, that didn't seem to be the lesson and scrolling down to the hints I saw that I'm supposed to use what we just learned.

I borrowed a bit of code from one of the lessons since I want to pass three functions through a function here, I think.

let luckyNumbers = [7, 4, 38, 21, 16, 15, 12, 33, 31, 49]
var oddNumbers = [Int]()
var sortedOddNumbers = [Int]()

func doImportantWork(first: () -> Void, second: () -> Void, third: () -> Void) {



doImportantWork {
    oddNumbers = luckyNumbers.filter { !$0.isMultiple(of: 2) }
} second: {
    sortedOddNumbers = oddNumbers.sorted()
} third: {
    let sortedOddNumbersMessage = { String($0) + " is a lucky number" }.joined(separator: "\n")


It works. But, this can't be right.



I don't think you were supposed to interpret .first { } and .second { } so literally.

I think he wants you to say luckyNumbers.filter { ... your code ... }.sorted().map { ... your code ... }

and then print out the result with a for loop.

More like your first answer but with only 1 or 2 lets.

Reworking your code, I get the following -

for row in luckyNumbers.filter({ !$0.isMultiple(of: 2) }).sorted().map({ "\($0) is a lucky number" }) {

Xcode helps clean up some warnings by putting parenthesis around closures - I don't really have an opinion on which is more readable.


I think I'm having a little trouble grasping these ideas the way they're explained since I'm coming from web languages.

If I understand this correctly, we're basically dealing with tons of pre-existing methods which we can pass data and other functions. Sort of like callbacks? And, we can even type in brief shorthand logic inside the closure braces if we don't want to make a function for it?



Yeah, I think functions are types just like strings and doubles so you can pass them wherever you want. And a closure is just an anonymous function defined inline. Which is helpful when the pre-existing methods don't do exactly what you want but you don't want to make a whole new named function.

The Swift syntax definitely is weird to me, though getting better the more hours I spend with it.


My take on it was the one below.

func transformAndPrintNumbers(_ numbers: [Int]) {
    let transformedNumbers = numbers
            .filter { !$0.isMultiple(of: 2) }
            .map { "\($0) is a lucky number" }

    for transformedNumber in transformedNumbers {

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


Thanks guys. That first example and the last one really helped me realize that what I wasn't grasping was the dot notation and the stringing together of the methods. That is so cool how that works.


Hacking with Swift is sponsored by Guardsquare

SPONSORED AppSweep by Guardsquare helps developers automate the mobile app security testing process with fast, free scans. By using AppSweep’s actionable recommendations, developers can improve the security posture of their apps in accordance with security standards like OWASP.

Learn more

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.