본문 바로가기

Swift Language

Swift Language - 1 - 기본 연산자 (Basic Operators)

할당 연산자(Assignment Operator)

let b = 10
var a = 5
a = b
// a 값은 10

let (x, y) = (1, 2)
// x 는 1, y 값은 2 가 됩니다.

사칙 연산자(Arithmetic Operators)

1 + 2       // 3
5 - 3       // 2
2 * 3       // 6
10.0 / 2.5  // 4.0

나머지 연산자(Remainder Operator)

9 % 4    // 1
-9 % 4   // -1

단항 음수 연산자(Unary Minus Operator)

let three = 3
let minusThree = -three       // minusThree는 -3
let plusThree = -minusThree   // plusThree는 3, 혹은 "minus minus 3"

단항 양수 연산자(Unary Plus Operator)

let minusSix = -6
let alsoMinusSix = +minusSix  // alsoMinusSix는 -6

합성 할당 연산자 (Compound Assignment Operators)

var a = 1
a += 2
// a는 3

삼항 조건 연산자(Ternary Conditional Operator)

if question {
    answer1
} else {
    answer2
}

let contentHeight = 40
let hasHeader = true
let rowHeight = contentHeight + (hasHeader ? 50 : 20)
// rowHeight는 90 (40 + 50)

Nil 병합 연산자(Nil-Coalescing Operator)

let defaultColorName = "red"
var userDefinedColorName: String?   // 이 값은 defaults 값 nil입니다.

var colorNameToUse = userDefinedColorName ?? defaultColorName
// userDefinedColorNam이 nil이므로 colorNameToUse 값은 defaultColorName인 "red"가 설정 됩니다.

userDefinedColorName = "green"
colorNameToUse = userDefinedColorName ?? defaultColorName
// userDefinedColorName가 nil이 아니므로 colorNameToUse 는 "green"이 됩니다.

닫힌 범위 연산자(Closed Range Operator)

// (a...b)의 형태로 범위의 시작과 끝이 있는 연산자 입니다.

for index in 1...5 {
    print("\(index) times 5 is \(index * 5)")
}
// 1 times 5 is 5
// 2 times 5 is 10
// 3 times 5 is 15
// 4 times 5 is 20
// 5 times 5 is 25

반 닫힌 범위 연산자(Half-Open Range Operator)

// (a..<b)의 형태로 a부터 b보다 작을 때까지의 범위를 갖습니다.

let names = ["Anna", "Alex", "Brian", "Jack"]
let count = names.count
for i in 0..<count {
    print("Person \(i + 1) is called \(names[i])")
}
// Person 1 is called Anna
// Person 2 is called Alex
// Person 3 is called Brian
// Person 4 is called Jack

단방향 범위(One-Side Ranges)

// [a..] [..a]의 형태로 범위의 시작 혹은 끝만 지정해 사용하는 범위 연산자 입니다. 
// 지정한 시작 값 혹은 끝 값은 범위에 포함됩니다.

for name in names[2...] {
    print(name)
}
// Brian
// Jack

for name in names[...2] {
    print(name)
}
// Anna
// Alex
// Brian

논리 부정 연산자(Logical NOT Operator)

let allowedEntry = false
if !allowedEntry {
    print("ACCESS DENIED")
}
// Prints "ACCESS DENIED"

논리 곱 연산자(Logical AND Operator)

let enteredDoorCode = true
let passedRetinaScan = false
if enteredDoorCode && passedRetinaScan {
    print("Welcome!")
} else {
    print("ACCESS DENIED")
}
// Prints "ACCESS DENIED"

논리 합(OR) 연산자(Logical OR Operator)

let hasDoorKey = false
let knowsOverridePassword = true
if hasDoorKey || knowsOverridePassword {
    print("Welcome!")
} else {
    print("ACCESS DENIED")
}
// Prints "Welcome!"

논리 연산자의 조합(Combining Logical Operators)

// 두 개 이상의 논리 연산자를 조합해서 사용할 수 있습니다. 
// Swift의 논리 연산자 && 와 || 는 왼쪽의 표현을 우선해서 논리 계산을 합니다.

if enteredDoorCode && passedRetinaScan || hasDoorKey || knowsOverridePassword {
    print("Welcome!")
} else {
    print("ACCESS DENIED")
}
// Prints "Welcome!"


// 괄호를 사용하면 가독성이 높아져 코드의 의도를 더 명확하게 하는데 도움이 됩니다.

if (enteredDoorCode && passedRetinaScan) || hasDoorKey || knowsOverridePassword {
    print("Welcome!")
} else {
    print("ACCESS DENIED")
}
// Prints "Welcome!"




출처
https://jusung.gitbook.io/the-swift-language-guide/language-guide/02-basic-operators