Swift inline conditional?

SwiftConditional

Swift Problem Overview


How do I do this in Swift ?

(someboolexpression ? "Return value 1" : "Return value 2")

(no I have not read the whole manual yet... I probably missed it on page 2!)

OK so its on page 91 and the above appears to be correct. However I am trying to use this in a string like so:

println(" some string \(some expression ? "Return value 1" : "Return value 2")"

but the compiler is not happy. Any idea if this if possible?

This is as close as I have been able to get

let exists = "exists"
let doesnotexist= "does not exist"
                
println("  something \(fileExists ? exists : doesnotexist)")

Swift Solutions


Solution 1 - Swift

If you're looking for a one-liner to do that, you can pull the ?: operation out of the string interpolation and concatenate with + instead:

let fileExists = false // for example
println("something " + (fileExists ? "exists" : "does not exist"))

Outputs:

> something does not exist

Solution 2 - Swift

You can use the new Nil-Coalescing Operator, introduced in Swift 3. It will return default value if someOptional is nil.

let someValue = someOptional ?? ""

Here, if someOptional is nil, this operator will assign "" to someValue.

Solution 3 - Swift

var firstBool = true
var secondBool: Bool

firstBool == true ? (secondBool = true) : (secondBool = false)

If in this case, it changes the secondBool to whatever the firstBool is. You can do this with integers and strings too

Solution 4 - Swift

It is called a "ternary operator".

With regards to @Esqarrouth's answer, I think a better format would be:

Swift 3:

var firstBool = true
var secondBool: Bool

secondBool = firstBool ? true : false

This is the same as:

var firstBool = true
var secondBool: Bool

if (firstBool == true) {
    secondBool = true
} else {
    secondBool = false
}

Solution 5 - Swift

You were oh so close. Just needed to assign it to a variable:

self.automaticOption = (automaticOptionOfCar ? "Automatic" : "Manual")
Edit:

> Any idea why that same expression can't be embedded in a string?

You can do that:

let a = true
let b = 1
let c = 2

println("\(a ? 1: 2)")

Solution 6 - Swift

Well,

If you concatenate the conditional with the string using the + operator, it should work.

Therefore, Mike is correct.

var str = "Something = " + (1 == 1 ? "Yes" : "No")

Solution 7 - Swift

simple solution i used in my projects

Swift 3+

var retunString = (state == "OFF") ? "securityOn" : "securityOff"

Solution 8 - Swift

I have use inline conditional like this :

isFavorite function returns a Boolen

favoriteButton.tintColor = CoreDataManager.sharedInstance.isFavorite(placeId: place.id, type: 0) ? UIColor.white : UIColor.clear

tourOperatorsButton.isHidden = place.operators.count != 0 ? true : false

Solution 9 - Swift

For Multiple Condition this can work like this

 let dataSavingTime: DataSavingTime = value == "0" ? .ThirtySecs : value == "1" ? .Timing1 : .Timing2

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDuncan GroenewaldView Question on Stackoverflow
Solution 1 - SwiftMike SView Answer on Stackoverflow
Solution 2 - SwiftVitalii GozhenkoView Answer on Stackoverflow
Solution 3 - SwiftEsqarrouthView Answer on Stackoverflow
Solution 4 - SwiftRay TsoView Answer on Stackoverflow
Solution 5 - SwiftSteve RosenbergView Answer on Stackoverflow
Solution 6 - SwiftTanmay BakshiView Answer on Stackoverflow
Solution 7 - SwiftSathya BamanView Answer on Stackoverflow
Solution 8 - SwiftSergio TrejoView Answer on Stackoverflow
Solution 9 - SwiftWaliyanView Answer on Stackoverflow