Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Swift/Swift Documents

[Swift] ๊ณต์‹ ๋ฌธ์„œ ์ •๋ฆฌ (2) - Basic Operators (๊ธฐ์ดˆ ์—ฐ์‚ฐ์ž)

๊ฐ์ž ๐Ÿฅ” 2022. 1. 7. 18:48
๋ฐ˜์‘ํ˜•

์‹œ์ž‘ํ•˜๋ฉฐ

swift ๊ณต๋ถ€๋ฅผ ์‹œ์ž‘ํ•œ์ง€ ์ดํ‹€์ฐจ, ์•ž์œผ๋กœ ๋งค์ผ ๊ณต๋ถ€ํ•ด๋ณด์ž! ๊ทธ๋Ÿผ ๋ฐ”๋กœ ๊ฐ€์žฅ ๊ธฐ์ดˆ๊ฐ€๋˜๋Š” ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•ด์„œ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ •๋ฆฌํ•ด๋ณด์ž. ์‚ฌ์‹ค ํ•œ๋ฒˆ ์‘ฅ ํ›‘์œผ๋ฉด ๋˜๋Š” ๊ฐœ๋…๋“ค์ด๊ธด ํ•ด์„œ ์ด๋ ‡๊ฒŒ ๋ฆฌ๋ทฐํ•˜๋Š”๊ฒŒ ์‹œ๊ฐ„๋‚ญ๋น„์ผ๊นŒ? ๋ผ๋Š” ์ƒ๊ฐ์ด ๋“ค๊ธฐ๋„ ํ•˜์ง€๋งŒ, ๋‚˜์ค‘์„ ์œ„ํ•ด ๊ธฐ์ดˆ๋ถ€ํ„ฐ ์ฐจ๊ทผ์ฐจ๊ทผ ๋ฐŸ์•„๋ณด๊ธฐ๋กœ ํ•˜์ž. ํŒŒ์ด์ฌ์€ ์ด๋Ÿฐ ๊ณต์‹๋ฌธ์„œ๋ฅผ ์ฝ์ง€๋„ ์•Š๊ณ  ์‹œ์ž‘ํ–ˆ๋Š”๋ฐ... ๋„์›€์ด ๋˜๊ฒŸ์ง€!! ์ง€๋ฃจํ•ด๋„ ์ข€๋งŒ ํž˜๋‚ด์ž. 

Swift ๊ณต์‹ Documents (2) - Basic operators

๋ฐ˜์‘ํ˜•
 

Basic Operators — The Swift Programming Language (Swift 5.5)

Basic Operators An operator is a special symbol or phrase that you use to check, change, or combine values. For example, the addition operator (+) adds two numbers, as in let i = 1 + 2, and the logical AND operator (&&) combines two Boolean values, as in i

docs.swift.org

 


Basic Operators (๊ธฐ์ดˆ ์—ฐ์‚ฐ์ž)

์—ฐ์‚ฐ์ž๋Š” ๊ฐ’์„ ํ™•์ธ, ๋ณ€๊ฒฝ, ๋˜๋Š” ๊ฒฐํ•ฉํ•˜๊ธฐ ํ•˜๋Š”๋ฐ ์‚ฌ์šฉํ•˜๋Š” ํŠน์ˆ˜ ๊ธฐํ˜ธ์ด๋‹ค. + ๋Š” ๋‘ ์ˆ˜๋ฅผ ๋”ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•˜๋Š” ์—ฐ์‚ฐ์ž์ด๊ณ , && ๋Š” AND ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. 

swift๋Š” C์–ธ์–ด์˜ ์ฝ”๋”ฉ ์˜ค๋ฅ˜๋ฅผ ์ œ๊ฑฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ธฐ๋Šฅ์„ ํ–ฅ์ƒ์‹œํ‚จ ์—ฐ์‚ฐ์ž๋ฅผ ์ง€์›ํ•œ๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด (=) ์—ฐ์‚ฐ์ž๋Š” ๊ฐ’์„ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š”๋ฐ ์ด๋Š” (==) ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๊ฐ’์ด ์ž˜๋ชป ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์„ ๋ง‰์„ ์ˆ˜ ์žˆ๋‹ค. 
์‚ฐ์ˆ ์—ฐ์‚ฐ์ž (_+, -, *, / , % ๋“ฑ)์€ ์˜ค๋ฒ„ํ”Œ๋กœ๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒํ•˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ํ—ˆ์šฉ๋œ ๊ฐ’ ๋ฒ”์œ„๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ์ž‘์€ ์ˆซ์ž๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ฒŒ ๋„์™€์ค€๋‹ค.

swift๋Š” C์—์„œ๋Š” ์ œ๊ณตํ•˜์ง€ ์•Š๋Š” a...<b, a...b ์™€ ๊ฐ™์€ ๋ฒ”์œ„ ์—ฐ์‚ฐ์ž๋ฅผ ์ œ๊ณตํ•œ๋‹ค. ์ด๋ฒˆ ๊ธ€์—์„œ๋Š” swift์˜ ๊ธฐ์ดˆ ์—ฐ์‚ฐ์ž์— ๋Œ€ํ•ด์„œ ์„ค๋ช…ํ•  ๊ฒƒ์ด๋‹ค.

 


Terminology (์šฉ์–ด)

์—ฐ์‚ฐ์ž๋Š” unary(๋‹จํ•ญ), binary(์ดํ•ญ) ๋˜๋Š” ternary (์‚ผํ•ญ)์ด๋‹ค. 

unary ์—ฐ์‚ฐ์ž๋Š” ํ•œ ๊ฐœ์˜ ๋Œ€์ƒ์ด ์žˆ๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค (์˜ˆ: -a).
Unary Prefix ์—ฐ์‚ฐ์ž๋Š” ๋Œ€์ƒ ๋ฐ”๋กœ ์•ž์— ๋‚˜ํƒ€๋‚˜๊ณ  (!a) Unary Postfix์—ฐ์‚ฐ์ž๋Š” ๋Œ€์ƒ ๋ฐ”๋กœ ๋’ค์— ๋‚˜ํƒ€๋‚œ๋‹ค. (a!)

binary ์—ฐ์‚ฐ์ž๋Š” ๋‘ ๊ฐœ์˜ ๋Œ€์ƒ์ด ์žˆ๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค. (a+b).

Ternary ์—ฐ์‚ฐ์ž๋Š” ์„ธ ๊ฐœ์˜ ๋Œ€์ƒ์ด ์žˆ๋Š” ์—ฐ์‚ฐ์ž์ด๋‹ค. swift์—์„œ๋Š” c์–ธ์–ด์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•˜๋‚˜์˜ ์‚ผํ•ญ ์—ฐ์‚ฐ์ž๊ฐ€ ์กด์žฌํ•œ๋‹ค.

a? b:c

๋ผ๊ณ  ์“ฐ์ด๋Š” ์กฐ๊ฑด ์—ฐ์‚ฐ์ž ๋งŒ์ด ์œ ์ผํ•œ Ternary ์—ฐ์‚ฐ์ž์ด๋‹ค.

์—ฐ์‚ฐ์ž์˜ ์˜ํ–ฅ์„ ๋ฐ›๋Š” ๊ฒƒ์€ ํ”ผ์—ฐ์‚ฐ์ž์ด๋‹ค. 1+2์—์„œ +๋Š” ์—ฐ์‚ฐ์ž์ด๊ณ , 1๊ณผ 2๋Š” ํ”ผ์—ฐ์‚ฐ์ž์ด๋‹ค. ๊ฒฐ๊ณผ๋Š” ํ”ผ์—ฐ์‚ฐ์ž์— ์˜ํ•ด ์˜ํ–ฅ์„ ๋ฐ›๋Š”๋‹ค.


Assignment operator (ํ• ๋‹น ์—ฐ์‚ฐ์ž)

ํ• ๋‹น ์—ฐ์‚ฐ์ž (=) ๋Š” ๊ฐ’์„ ์ดˆ๊ธฐํ™” ํ•˜๊ฑฐ๋‚˜ ์—…๋ฐ์ดํŠธํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

let b = 10
var a = 5

a=b
print(a)
// 10

๋งŒ์•ฝ ์—ฌ๋Ÿฌ ๊ฐ’์„ ๊ฐ€์ง„ ํŠœํ”Œ์ธ ๊ฒฝ์šฐ ํ•ด๋‹น ์›์†Œ๋ฅผ ์œ„์˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด ํ•œ ๋ฒˆ์— ์—ฌ๋Ÿฌ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๋กœ ๋ถ„ํ•ดํ•  ์ˆ˜ ์žˆ๋‹ค. 

let (x, y) = (1, 2)
// x is equal to 1, and y is equal to 2

print(x)
// 1

C์–ธ์–ด์˜ ์—ฐ์‚ฐ์ž์™€๋Š” ๋‹ฌ๋ฆฌ swift์˜ ์—ฐ์‚ฐ์ž๋Š” ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š”๋‹ค. ๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์€ ์‹์€ ์œ ํšจํ•˜์ง€ ์•Š๋‹ค.

if x = y {
    // This isn't valid, because x = y doesn't return a value.
}

์ด๋ ‡๊ฒŒ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š” ๊ธฐ๋Šฅ์€, ์—ฐ์‚ฐ์ž == ๋“ฑํ˜ธ์™€ ์‹ค์ˆ˜๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•œ๋‹ค.


Arithmetic Operators (์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž)

swift๋Š” 4๊ฐœ์˜ ํ‘œ์ค€ ์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž๋ฅผ ์ง€์›ํ•œ๋‹ค.

  • Addition (+)
  • Subtraction (-)
  • Multiplication (*)
  • Division (/)
1 + 2       // equals 3
5 - 3       // equals 2
2 * 3       // equals 6
10.0 / 2.5  // equals 4.0

C, objective-C์˜ ์‚ฐ์ˆ ์—ฐ์‚ฐ์ž์™€๋Š” ๋‹ฌ๋ฆฌ swift์—ฐ์‚ฐ์ž๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ’์ด ์˜ค๋ฒ„ํ”Œ๋กœ ๋˜๋Š” ๊ฒƒ์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š๋Š”๋‹ค. swift์—์„œ ์˜ค๋ฒ„ํ”Œ๋กœ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์˜ค๋ฒ„ํ”Œ๋กœ๊ฐ€ ๋ฐœ์ƒํ–ˆ์„ ๋•Œ ์‹คํ–‰์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์ด๋Š” ์ถ”ํ›„ Advanced operator ๋‹จ์›์—์„œ ๊ณต๋ถ€ํ•ด๋ณผ ์ˆ˜ ์žˆ๋‹ค. 

์‚ฐ์ˆ  ์—ฐ์‚ฐ์ž๋Š” string์—์„œ๋„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

"hello, " + "world"  // equals "hello, world"

 

Remainder Operator (๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž)

rimainder operator์€ ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž๋กœ a%b ์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. a๋ฅผ b๋กœ ๋‚˜๋ˆ„์—ˆ์„ ๋•Œ์˜ ๋‚˜๋จธ์ง€๋ฅผ ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋‚˜๋จธ์ง€๊ฐ€ ์˜ค๊ฒŒ๋˜๋Š” ์›๋ฆฌ๋Š” [ a = (b*๊ณฑํ•ด์งˆ ๊ฐ’) + ๋‚˜๋จธ์ง€ ] ์ด๋‹ค. ์—ฌ๊ธฐ์„œ ๊ณฑํ•ด์งˆ ๊ฐ’์€ a๋ฅผ b๋กœ ๋‚˜๋ˆˆ ๋ชซ์ด ๋œ๋‹ค.

๋งŒ์•ฝ a = -9, b = 4๋ผ๋ฉด -9 = (4*2) + (-1) ์ด ๋˜๊ธฐ์—, ๋‚˜๋จธ์ง€๋Š” -1 ์ด ๋œ๋‹ค. ๊ณ„์‚ฐ์‹œ b์˜ ๋ถ€ํ˜ธ๋Š” ๋ฌด์‹œ๋˜๊ธฐ ๋•Œ๋ฌธ์— a%b๋‚˜ a%-b์˜ ๊ฐ’์€ ๊ฐ™์„ ๊ฒƒ์ด๋‹ค.

print(-9%4) // -1

 

Unary Minus Operator (๋‹จํ•ญ ๋งˆ์ด๋„ˆ์Šค ์—ฐ์‚ฐ์ž)

์ˆซ์ž ์•ž์— ๋ถ™์–ด ๋ฐ”๋กœ ํ•ด๋‹น ์ˆซ์ž์— -1 ์„ ๊ณฑํ•ด์ค€๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ž.

let three = 3
let minusThree = -three       // minusThree equals -3
let plusThree = -minusThree   // plusThree equals 3, or "minus minus three"

 

Unary Plus Operator (๋‹จํ•ญ ํ”Œ๋Ÿฌ์Šค ์—ฐ์‚ฐ์ž)

์ˆซ์ž ์•ž์— ๋ถ™์–ด ํ•ด๋‹น ์ˆซ์ž์— +1์„ ๊ณฑํ•ด์ค€๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ž.

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

Compound Assignment Operators (๋ณตํ•ฉ ํ• ๋‹น ์—ฐ์‚ฐ์ž)

C์–ธ์–ด๋ž‘ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ Swift์—์„œ๋„ ๋ณตํ•ฉํ• ๋‹น์—ฐ์‚ฐ์ž๋ฅผ ์ œ๊ณตํ•œ๋‹ค. += ์™€ ๊ฐ™์€ ์—ฐ์‚ฐ์ž๊ฐ€ ๋ณตํ•ฉ ํ• ๋‹น ์—ฐ์‚ฐ์ž์ด๋‹ค.

var a = 1
a += 2
// a is now equal to 3

Comparison Operators (๋น„๊ต ์—ฐ์‚ฐ์ž)

==, !=, >, <, >=, <= ์™€ ๊ฐ™์€ ๋น„๊ต ์—ฐ์‚ฐ์ž๊ฐ€ ์กด์žฌํ•œ๋‹ค. Swift์—์„œ๋Š” ===, !==๋„ ์žˆ๋Š”๋ฐ ๋‘ ๊ฐ์ฒด ์ฐธ์กฐ๊ฐ€ ๋ชจ๋‘ ๋™์ผํ•œ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•˜๋Š”์ง€๋ฅผ ์•Œ๋ ค์ค€๋‹ค. ์–ด์จ‹๋“ , ๋น„๊ต ์—ฐ์‚ฐ์ž๋Š” Bool ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  • Equal to (a == b)
  • Not equal to (a != b)
  • Greater than (a > b)
  • Less than (a < b)
  • Greater than or equal to (a >= b)
  • Less than or equal to (a <= b)
1 == 1   // true because 1 is equal to 1
2 != 1   // true because 2 isn't equal to 1
2 > 1    // true because 2 is greater than 1
1 < 2    // true because 1 is less than 2
1 >= 1   // true because 1 is greater than or equal to 1
2 <= 1   // false because 2 isn't less than or equal to 1

๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— if๋ฌธ๊ณผ ๊ฐ™์€ ์กฐ๊ฑด๋ฌธ์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

let name = "world"
if name == "world" {
    print("hello, world")
} else {
    print("I'm sorry \(name), but I don't recognize you")
}
// Prints "hello, world", because name is indeed equal to "world".

๋™์ผํ•œ ํƒ€์ž…, ๋™์ผํ•œ ๊ฐœ์ˆ˜๋ฅผ ๊ฐ€์ง„ ํŠœํ”Œ ํ˜•์‹์„ ๋น„๊ตํ•  ์ˆ˜๋„ ์žˆ๋Š”๋ฐ, ํŠœํ”Œ์„ ๋น„๊ตํ•  ๋• ์™ผ์ชฝ์—์„œ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ํ•œ ๊ฐ’์”ฉ ๋น„๊ตํ•˜๊ฒŒ ๋œ๋‹ค.

(1, "zebra") < (2, "apple")   // true because 1 is less than 2; "zebra" and "apple" aren't compared
(3, "apple") < (3, "bird")    // true because 3 is equal to 3, and "apple" is less than "bird"
(4, "dog") == (4, "dog")      // true because 4 is equal to 4, and "dog" is equal to "dog"

์ฆ‰ ์™ผ์ชฝ ๋ถ€ํ„ฐ ํ•œ ๊ฐ’์”ฉ ๋น„๊ตํ•œ๋‹ค๋Š” ๋ง์€, (1, zebra) < (2, apple) ์—์„œ ์™ผ์ชฝ๋ถ€ํ„ฐ ๋ณด๊ธฐ ๋•Œ๋ฌธ์— 1, 2 ๋ฅผ ๋น„๊ตํ•˜๊ฒŒ ๋˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ 2๊ฐ€ ๋” ํฌ๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ”๋กœ true๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ ๋™์ผํ•˜๋ฉด ์˜ค๋ฅธ์ชฝ ์›์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ๋น„๊ตํ•˜๊ฒŒ ๋œ๋‹ค.

("blue", -1) < ("purple", 1)        // OK, evaluates to true
("blue", false) < ("purple", true)  // Error because < can't compare Boolean values

์œ„ ์ฝ”๋“œ์—์„œ, Bool๊ฐ’์€ ๋Œ€์†Œ ๋น„๊ต๊ฐ€ ์•ˆ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.


Ternary conditional Operator (์‚ผํ•ญ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž) 

์‚ผํ•ญ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๋Š” ์„ธ ๋ถ€๋ถ„์œผ๋กœ ๋œ ํŠน์ˆ˜ ์—ฐ์‚ฐ์ž๋กœ ์•„๋ž˜์™€ ๊ฐ™์€ ํ˜•ํƒœ๋ฅผ ์ทจํ•œ๋‹ค.

? answer 1 : answer 2

์งˆ๋ฌธ์ด ์ฐธ์ธ์ง€ ๊ฑฐ์ง“์ธ์ง€์— ๋”ฐ๋ผ ๋‘ ๊ฐ€์ง€ ์ค‘์— ํ•˜๋‚˜๋ฅผ ํ‰๊ฐ€ํ•œ๋‹ค. ์งˆ๋ฌธ์ด ์ฐธ์ด๋ฉด answer1์„ ํ‰๊ฐ€ํ•˜์—ฌ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด (์งˆ๋ฌธ์ด ๊ฑฐ์ง“์ด๋ฉด) answer2๋ฅผ ํ‰๊ฐ€ํ•˜์—ฌ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. 

if question {
    answer1
} else {
    answer2
}

์œ„์˜ if๋ฌธ์„ ์‚ผํ•ญ ์กฐ๊ฑด ์—ฐ์‚ฐ์ž๋กœ ํ‘œํ˜„ํ•œ ์˜ˆ์‹œ๊ฐ€ ์•„๋ž˜์— ์žˆ๋‹ค. 

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

//์‚ผํ•ญ ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ โ–ผ
let contentHeight = 40
let hasHeader = true
let rowHeight: Int
if hasHeader {
    rowHeight = contentHeight + 50
} else {
    rowHeight = contentHeight + 20
}
// rowHeight is equal to 90

hasHeader? 50:20์ด๋ผ๋Š” ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๋˜๋Š”๋ฐ ์ด๋•Œ hasHeader๊ฐ€ true์ด๋ฏ€๋กœ 50์ด ๋ฐ˜ํ™˜๋˜์–ด rowHeight์— 50์ด ์ €์žฅ๋œ๋‹ค.


Nil-Coalescing Operator (?? : ์˜ต์…”๋„ ๋ถ„์„ ์—ฐ์‚ฐ์ž)

nil coalescing operator์€ a??b ์™€ ๊ฐ™์€ ์—ฐ์‚ฐ์ž๋กœ ์˜ต์…”๋„์„ ๋ถ„์„ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์˜ต์…”๋„ ๋ณ€์ˆ˜ a๊ฐ€ ์žˆ๊ณ  a??b๊ฐ€ ์žˆ๋‹ค๋ฉด, ๋งŒ์•ฝ a๊ฐ€ nil๊ฐ’์ด๋ฉด b๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ์•„๋‹Œ ๊ฒฝ์šฐ ๊ทธ๋Œ€๋กœ ๊ทธ๋ƒฅ a๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๋•Œ ๋ฐ˜๋“œ์‹œ a๋Š” ์˜ต์…”๋„ ํƒ€์ž…์ด์–ด์•ผ ํ•œ๋‹ค. 

ํ’€์–ด์„œ ์“ฐ๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™๊ณ , ์ด๋ฅผ ?? ํ™œ์šฉํ•œ ์˜ˆ์‹œ๋„ ๋„ฃ์–ด ๋ณด์•˜๋‹ค.

// ํ’€์–ด์“ฐ๋Š” ๊ฒฝ์šฐ
a != nil ? a! : b


// ?? ์˜ต์…”๋„ ์‚ฌ์šฉํ•œ ๊ฒฝ์šฐ
let defaultColorName = "red"
var userDefinedColorName: String?   // defaults to nil

var colorNameToUse = userDefinedColorName ?? defaultColorName
// userDefinedColorName is nil, so colorNameToUse is set to the default of "red"

Range Operator (๋ฒ”์œ„ ์—ฐ์‚ฐ์ž)

swift์—๋Š” ์—๋Š” ๊ฐ’ ๋ฒ”์œ„๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•œ ์—ฌ๋Ÿฌ ๋ฒ”์œ„ ์—ฐ์‚ฐ์ž๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.

 

Closed Range Operator (...)

closed range operator์€ a...b์™€ ๊ฐ™์ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์œผ๋กœ a์™€ b ์‚ฌ์ด์˜ ๊ฐ’์„ ํฌํ•จํ•˜๋Š”๋ฐ ์ด๋•Œ a์˜ ๊ฐ’์ด b๋ณด๋‹ค ํฌ๋ฉด ์•ˆ๋œ๋‹ค. ์ด๋Ÿฌํ•œ ์—ฐ์‚ฐ์ž๋Š” for-in ๋ฃจํ”„์™€ ๊ฐ™์ด ๋ชจ๋“  ๊ฐ’์„ ์‚ฌ์šฉํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

for-in ๊ตฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™๋‹ค. (a์ด์ƒ b์ดํ•˜ ๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ์ข‹๋‹ค. 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 (..<)

half open range operator์€ a..<b ์™€ ๊ฐ™์ด ์‚ฌ์šฉ๋œ๋‹ค. a์™€ b์‚ฌ์ด์˜ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋Š”๋ฐ b๋Š” ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค. (a์ด์ƒ b๋ฏธ๋งŒ)
์ด ์—ฐ์‚ฐ์ž๋Š” zero based list๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค

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-Sided Ranges ( ๋Œ€๊ด„ํ˜ธ ์•ˆ์— ๋ฒ”์œ„ [a...b] )

ํ•œ ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ์ปค์ง€๊ฑฐ๋‚˜ ์ž‘์•„์ง€๋Š” ๊ฐœ๋…์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด names ๋ฐฐ์—ด์— ๋“ค์–ด์žˆ๋Š” 2๋ฒˆ์งธ ์ธ๋ฑ์Šค ๊ฐ’๋ถ€ํ„ฐ ์ถœ๋ ฅํ•˜๊ฑฐ๋‚˜, 2๋ฒˆ์งธ ์ธ๋ฑ์Šค๊นŒ์ง€ ์ถœ๋ ฅํ•˜์˜€๋‹ค. (swift์—์„œ๋„ ์ธ๋ฑ์Šค ๋ฐฐ์—ด์€ 0๋ถ€ํ„ฐ ์‹œ์ž‘)

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

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

half-open range operator๋„ ํ•จ๊ป˜ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค. ์•„๋ž˜์™€ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. (2๋ฒˆ์งธ ์ธ๋ฑ์Šค ๋ฏธ๋งŒ ๊นŒ์ง€ ์ถœ๋ ฅ๋œ๋‹ค.)

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

์•„๋ž˜ ์ฝ”๋“œ์™€ ๊ฐ™์ด .contains๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํ•ด๋‹น ๋ฒ”์œ„์•ˆ์— ๋ณ€์ˆ˜๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

let range = ...5
range.contains(7)   // false
range.contains(4)   // true
range.contains(-1)  // true

 


Logical Operators (๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž)

๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋Š” boolean ๋…ผ๋ฆฌ ๊ฐ’์„ true๋‚˜ false๋กœ ๋ฐ”๊ถˆ์ค€๋‹ค. swift๋Š” ์˜ค๋กœ์ง€ ์•„๋ž˜์˜ ์„ธ ๊ฐœ์˜ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋งŒ ์ œ๊ณตํ•œ๋‹ค.

  • Logical NOT (!a)
  • Logical AND (a && b)
  • Logical OR (a || b)

 

Logical NOT operator

!a ๋กœ ์“ฐ์ด๊ณ , True๋Š” flase๋กœ, false๋Š” true๋กœ ๋ฐ”๊พธ์–ด์ค€๋‹ค. ๋ณ€์ˆ˜ ์•ž์— ์“ฐ๋ฉด ๋œ๋‹ค. (!a)

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

 

Logical AND operator

a&&b ๋กœ ์“ฐ์ด๋ฉฐ, a,b ๋ชจ๋‘ true์ผ๋•Œ๋งŒ true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ํ•˜๋‚˜๋ผ๋„ false์ธ ๊ฒฝ์šฐ false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

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

 

Logical OR operator

a || b ์™€ ๊ฐ™์ด ์“ฐ์ด๋ฉฐ, ๋‘˜ ์ค‘ ํ•˜๋‚˜๋ผ๋„ True ๋ฉด True๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋งŒ์•ฝ a ๊ฐ€ true๋ฉด b๋Š” ํ™•์ธ๋„ ํ•˜์ง€ ์•Š๊ณ  True๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

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

 

Combining Logical Operators

๊ฐœ๋ฐœ์ž๋Š” ์ด๋Ÿฐ ์—ฌ๋Ÿฌ ์—ฐ์‚ฐ์ž๋ฅผ ์กฐํ•ฉํ•ด์„œ ํ•จ๊ป˜ ๋ณ‘ํ•ฉํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

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

์—ฌ๋Ÿฌ && ๊ณผ || ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๋ณตํ•ฉ ํ‘œํ˜„์‹์„ ์ž‘์„ฑํ–ˆ๋‹ค. ์—ฌ๋Ÿฌ ๊ฐœ๋กœ ์ž‘์„ฑํ•ด๋„ ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž๋Š” ๋‘๊ฐœ ์”ฉ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํฌ๊ฒŒ 3๊ฐœ์˜ ์‹์œผ๋กœ ์ด๋ฃจ์–ด์ ธ์žˆ๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค. enteredDoorCode && passedRetinaScan์˜ ๊ฐ’์€ false๊ฐ€ ๋‚˜์˜ค๊ณ  false || hasDoorKey๋„ false๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜๋Š”๋ฐ ๊ฒฐ๊ตญ false || knowOverridePassword๋ฅผ ์—ฐ์‚ฐํ•˜๊ฒŒ ๋˜๋ฉด true๊ฐ€ ๋‚˜์™€์„œ Welcome! ์ด ์ถœ๋ ฅ๋˜๊ฒŒ ๋œ๋‹ค. ์ฆ‰, swift๋Š” ์™ผ์ชฝ์— ์žˆ๋Š” ํ•˜์œ„ ํ‘œํ˜„์‹์„ ๋จผ์ € ์—ฐ์‚ฐํ•œ๋‹ค.


Explicit Parentheses (๋ช…์‹œ์  ๊ด„ํ˜ธ)

๋ช…์‹œ์  ๊ด„ํ˜ธ๋Š” ๋ณต์žกํ•œ ์‹์„ ๋ณด๊ธฐ ์‰ฝ๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์‚ฌ์šฉ๋œ๋‹ค. ์œ„ ์ฝ”๋“œ๋ฅผ ๊ด„ํ˜ธ๋ฅผ ๋„ฃ์–ด์„œ ํ‘œํ˜„ํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

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

๊ด„ํ˜ธ๋Š” ์ฒ˜์Œ ๋‘ ๊ฐ’์ด ์ „์ฒด ๋…ผ๋ฆฌ์—์„œ ๊ฐ€์žฅ ๋จผ์ € ์ˆ˜ํ–‰ํ•ด์•ผ๋  ์—ฐ์‚ฐ์ด๋ผ๊ณ  ๋ช…ํ™•ํ•˜๊ฒŒ ํ‘œ์‹œํ•ด์ค€๋‹ค. ๋…์ž๋“ค์ด ๋” ๋ช…ํ™•ํ•˜๊ฒŒ ์ดํ•ดํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ๊ฐ„๊ฒฐํ•จ ๋ณด๋‹ค๋Š” ๊ฐ€๋…์„ฑ์— ์ดˆ์ ์„ ๋‘” ์ฝ”๋“œ๋‹ค. ์˜๋„๋ฅผ ๋ช…ํ™•ํ•˜๊ฒŒ ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด ๊ด„ํ˜ธ๋ฅผ ์ ์ ˆํžˆ ์‚ฌ์šฉํ•˜์ž.

 

๋งˆ๋ฌด๋ฆฌํ•˜๋ฉฐ

๊ธฐ์ดˆ์ ์ธ ์ง€์‹์ด๊ณ  , ๋‹ค๋ฅธ ์–ธ์–ด์™€ ๋น„์Šทํ•œ ์ ์ด ๋งŽ์•„ ์•„์ง๊นŒ์ง€๋Š” ๊ณต์‹ ๋ฌธ์„œ์ •๋ฆฌํ•˜๋Š” ๊ฒƒ์ด ์‹œ๊ฐ„๋‚ญ๋น„๋Š” ์•„๋‹๊นŒ? ์ƒ๊ฐ์ด ๋“ค๊ธฐ๋„ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ, ์ถ”ํ›„์— ์ฝ”๋”ฉ์„ ํ•˜๋‹ค๋ณด๋ฉด ๊ธฐ์ดˆ์ ์ธ ๋ถ€๋ถ„์—์„œ '์ด๊ฒŒ ์™œ ์˜ค๋ฅ˜๊ฐ€๋‚˜๋Š”๊ฑฐ์•ผ ์ •๋ง!!' ํ•˜๊ณ  ์งœ์ฆ๋‚˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์ด ๋ฐœ์ƒํ•˜๊ฒŒ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๋„์›€์ด ํ™•์‹คํžˆ!!!! ๋  ๊ฒƒ์ด๋ผ๊ณ  ๋ฏฟ๊ณ  ์žˆ๋‹ค. ์•„์ง ๊ธฐ์ดˆ ๋‹จ์›์ด๋ผ ๊ทธ๋Ÿฐ ๊ฒƒ์ผ ๊ฒƒ ๊ฐ™๊ณ  ๋’ค์— ์–ด๋ ค์šด ๋ถ€๋ถ„์— ๊ธฐ์ดˆ์ •๋ฆฌ๋Š” ์ •๋ง ๋„์›€์ด ๋  ๊ฒƒ ๊ฐ™๋‹ค. ๋๊นŒ์ง€ ๊ฐ€๋ณด์ž์•„์•„!!!!

๋ฐ˜์‘ํ˜•