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

SOLVED: A simple question about the ternary operator

Forums > 100 Days of SwiftUI

hello, I really like the ternary operator as it makes the code so much easier to read. However I have a problem with some more complex expressions, as the Swift compiler complains: here's my code:

element1.hasSuffix("a") || element2.hasSuffix("a") ? return true : return element1 < element2

This is just a variation of one of the exercises in "Closures" chapter of the Hacking With Swift UI course.

1      

Try

return ( element1.hasSuffix("a") || element2.hasSuffix("a") ? true : element1 < element2 )

1      

@Green has the correct answer.

Paste into Playgrounds for more clarification.

import SwiftUI

let pizzaOption = "margarhita"
let pastaOption = "carbonara"

// The first part of a ternary operation determines true or false
let hasSuffixTest = pizzaOption.hasSuffix("g") || pastaOption.hasSuffix("z")  // Will return true or false

// Second part of a ternary operation determines what to return. In your case, another boolean.
let ternaryResult = hasSuffixTest ? true : pizzaOption < pastaOption

// But a ternary operation could return a Color, or a string, or a function!
let colorResult  = hasSuffixTest ? Color.red : Color.teal
let stringResult = hasSuffixTest ? "✅ Suffix found!" : "Inconclusive 🤨"

// A ternary operation must return a valid type. What did you try to return?

If you review your code, you were trying to return unnecessary syntax, namely: return true and return element1 < element2

// This won't work!  
element1.hasSuffix("a") || element2.hasSuffix("a") ? return true : return element1 < element2

2      

Thank you! Now I understand my error.. I actually thought that the operator is like condition:whatiftrue?whatiffalse

1      

Arkadiusz moves forward!

Thank you! Now I understand my error..

Arkadiusz: You're welcome.

Good Luck! I hope I didn't give you any impressions that I thought you wouldn't understand. I enjoy answering questions and thinking of detailed answers. I enjoy thinking of different ways to explore and explain Swift, design, or architecture concepts that may give new developers heartache. However I've received feedback that my answers are perceived to be condescending, perhaps snarky. I want to remove heartache, not cause it.

I wish you luck on your journey. I will be taking a break from these forums.

Please mark @Green's answer as solved.

1      

I actually thought that the operator is like condition:whatiftrue?whatiffalse

It is. Your error was that you can't stick return statements in the whatiftrue and whatiffalse parts.

(Well, it's condition?whatiftrue:whatiffalse but I knew what you meant ;) )

1      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out 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.