GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

SOLVED: Checkpoint 5 Xcode warning

Forums > 100 Days of SwiftUI

Hi everyone, I already finished the checkpoint 5 with this solution bellow without warnings in Xcode:

import Cocoa

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

let checkPoint5 = { (luckyNumber: [Int]) in
        luckyNumber.filter {!($0.isMultiple(of: 2))}
        .sorted()
        .map { print("\($0) is a lucky number") }
}

checkPoint5(luckyNumbers)

So I tried to put the return Void after the Closure parameter, but I received the follow warning in Xcode: Result of call to 'map' is unused

import Cocoa

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

let checkPoint5 = { (luckyNumber: [Int]) -> Void in
        luckyNumber.filter {!($0.isMultiple(of: 2))}
        .sorted()
        .map { print("\($0) is a lucky number") }  //the warning is here
}

checkPoint5(luckyNumbers)

I'm just curious, why this happened when I put the Void return? I cant figure out why.

3      

Because your code is returning a value but you are telling the compiler that your closure doesn't return anything.

This code:

luckyNumber.filter {!($0.isMultiple(of: 2))}
.sorted()
.map { print("\($0) is a lucky number") } 

returns [Void], not Void

4      

You have an incompatible closure declaration and usage.

When you use -> Void, you are telling the compiler that the function, or in this case the closure, does not return a result.

for example

func printMessage(_ theMessage: String) {   // no Void is used here
    print("Message: \(theMessage)")
}

let outputMessage: (String) -> Void = outputMessage
outputMessage ("A void message")

Conversely, when there is a result returned ( in this case the result of the filtering, sorting and mapping), but the closure (or function) declares -> Void, then effectively you are trying to discard the result, when maybe you shouldn't. Hence the warning that the result (of the last modifier - .map) is unused.

The compiler does not throw an error, because it can in this case ignore the result from the closure, however giving you a warning to check your code to make sure it is as you intended.

4      

the map method's return value is not Void. You can instead use the method forEach.

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

luckyNumbers.filter { !$0.isMultiple(of: 2) }
.sorted { $0 < $1 }
.map { "\($0) is a lucky number" }
.forEach { print($0) }

3      

Hacking with Swift is sponsored by Essential Developer.

SPONSORED Transform your career with the iOS Lead Essentials. Unlock over 40 hours of expert training, mentorship, and community support to secure your place among the best devs. Click for early access to this limited offer and a FREE crash course.

Click to save your free spot now

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.