What version of Xcode are you using? Note that using if as an expression is new in Swift 5.9 and requires Xcode 15. If you are in Xcode 14 or earlier, then you can use one of the solutions given above.
Unfortunately the default language book on Swift.org is for 5.9 already and I can’t even find an older version.
Topic:
Programming Languages
SubTopic:
Swift
Tags: