Josh O'Connor Josh O'Connor - 2 years ago 68
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 Source

This is a short hand if else statement.

if isInverted {
   return !wasInitiallyAdded
else {
   return wasInitiallyAdded
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download