UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

2      

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

3      

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.

3      

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) }

2      

Hacking with Swift is sponsored by RevenueCat

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

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

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.