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

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.

   

Try

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

   

@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

1      

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

   

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.

   

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

   

Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.