Swift Programming Language Guide: Basic Operators. 코드부분
/* Ternary Conditional Operator */
// The ternary conditonal operator is a special operator with three parts, which takses the form question ? answer 1 : answer2.
if question {
answer1
} else {
answer2
}
let contentHeight = 40
let hasHeader = true
let rowHeight = contentHeight + (hasHeader ? 50 : 20)
let contentHeigth = 40
let hasHeader = true
let rowHeight: Int
if hasHeader {
rowHeight = contentHeigth + 50
} else {
rowHeight = contentHeigth + 20
}
/* Nil-Coalescing Operator */
//The nil-coalescing operator (a ?? b) unwraps an optional a if it contains a value, or returns a default value b if a is nil.
a != nil ? a! : b
let defaultColorName = "red"
var userDefinedColorName: String?
var colorNameToUse = userDefinedColorName ?? defaultColorName
userDefinedColorName = "green"
colorNameToUse = userDefinedColorName ?? defaultColorName