Josh O'Connor Josh O'Connor - 5 months ago 11
Swift Question

Unclear understand of ? : in swift syntax

I can't quite comprehend this syntax in the return statement. I'm not sure if it is new in Swift 2.0, but what does the ? and the : mean? Is this question mark an optional, even though spaced? Im quite confused, coming from an Objective-C background.

private func doContainsUser(user: User) -> Bool {
let isInverted = setOfDiff.contains(user)
let wasInitiallyAdded = setOfCircleUsers.contains(user)

//What does the ? and the : mean?
return isInverted ? !wasInitiallyAdded : wasInitiallyAdded
}

Answer

This is a short hand if else statement.

if isInverted {
   return !wasInitiallyAdded
}
else {
   return wasInitiallyAdded
}