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

Returning values test

Forums > 100 Days of SwiftUI

Hello everyone,

I couldnt understand the 5th questions logic since, the top questions says "this code is valid swift - true or false -.

func paintHouse (color: String) -> Bool { if color == "tartan" { return false } }

i think this is valid swift! but paul says: "Oops - that's not correct. This does not return a value if color is not "tartan"."

but the color might me tartan so code is valid?

what are your thoughts?

2      

-> Bool

The function returns a Bool and only has an if … then, for one result for the boolean. It needs to be if … then … else to return the full boolean.

if color == "tartan" { return false }

needs to be

if color == "tartan" { return false } else { return true }

for it to be correct code.

3      

You could do, as the color == "tarten" will produce a Bool of true, however you want the fasle so need to use !=

func paintHouse(color: String) -> Bool { color != "tartan" }

3      

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.

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.