ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

Swift

[Swift] Docs_(1) The Basics

Horang๐Ÿฏ 2024. 1. 23. 17:06

์š”์ฆ˜ ๋งค์ผ ์ฑ…์ƒ์— ์•‰์•„ ๊ณต๋ถ€๋ฅผ ํ•œ๋‹ค๊ณ  ํ•˜๋Š”๋ฐ,

์ด๊ฒƒ๋„ ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™๊ณ , ์ €๊ฒƒ๋„ ํ•ด์•ผํ•  ๊ฒƒ ๊ฐ™์€ ๋งˆ์Œ์— ์ž๋ฃŒ๋ฅผ ์ฐพ๋Š” ๋ฐ์—๋งŒ ๋งŽ์€ ์‹œ๊ฐ„์„ ์“ฐ๊ณ 

์ •์ž‘ ์ œ๋Œ€๋กœ ๋œ ๊ณต๋ถ€๋Š” ๋ชปํ•˜๊ณ  ์žˆ๋‹ค๋Š” ์ƒ๊ฐ์ด ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋ž˜์„œ, ํ•˜๋‚˜๋ฅผ ์ •ํ•˜๊ณ 

์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ง„ํ–‰ํ•ด๋ณด๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.

 

๊ณต๋ถ€ํ•  ๊ฒƒ์€ ๋ฐ”๋กœ, Swift ๋ฌธ๋ฒ•์— ๋Œ€ํ•ด์„œ ๊ณต๋ถ€ํ•˜๊ณ  ์‹ถ์–ด,

Swift ๊ณต์‹ ๋ถ„์„œ๋ฅผ ์ •๋…ํ•˜๊ณ ์ž ์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

(๋ฌธ๋ฒ•์„ ๊ณ„์† ๋‹ค์‹œ ์ฐพ์•„๋ณด์ž๋‹ˆ.. ๋‹ค์‹œ ์ œ๋Œ€๋กœ๋œ ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค.๐Ÿ˜ญ)

 

์•„๋งˆ, ๋‹ค๋ฅธ ๊ฒƒ๋“ค๋„ ํ•จ๊ป˜ ๊ณต๋ถ€๋ฅผ ํ•˜๊ณ  ์žˆ์–ด

์™„๋…ํ•˜๋Š” ๊ฒƒ์ด ์‰ฝ์ง€๋Š” ์•Š๊ฒ ์ง€๋งŒ, ๊พธ์ค€ํžˆ ๋‹ค! ์˜ฌ๋ ค๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค


์ด๋ฒˆ ๊ธ€์€ ์•„๋ž˜ Swift ๊ณต์‹ ๋ฌธ์„œ์˜ ์ฒซ ๋ฒˆ์งธ ๋‹จ์›์ธ <The Basics>๋ฅผ ๋ณด๊ณ 

์ „์ฒด์ ์ธ ๋‚ด์šฉ๊ณผ ์ €์˜ ์ƒ๊ฐ์„ ๊ธฐ๋กํ•ด๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

 

Swift ๊ณต์‹ ๋ฌธ์„œ <The Basics>

 

์ค‘๊ฐ„ ์ค‘๊ฐ„ ์ €์˜ ์ƒ๊ฐ์ด ํฌํ•จ๋  ์ˆ˜ ์žˆ์–ด

ํ˜น์‹œ๋‚˜ ์ž˜๋ชป๋œ ๋‚ด์šฉ์ด ์žˆ์œผ๋ฉด ๋Œ“๊ธ€๋กœ ํ›ˆ์ˆ˜ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๐Ÿค“๐Ÿค“๐Ÿค“


Swift๋Š” ๋‹ค์–‘ํ•œ ๊ธฐ๋ณธ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

  • ์ •์ˆ˜์— ๋Œ€ํ•œ Int
  • ๋ถ€๋™ ์†Œ์ˆ˜์  ๊ฐ’์— ๋Œ€ํ•œ Double
  • ๋ถˆ๋ฆฐ ๊ฐ’(์ฐธ, ๊ฑฐ์ง“)์„ ์œ„ํ•œ Bool
  • ํ…์ŠคํŠธ์— ๋Œ€ํ•œ String
  • Collection Type  (Array, Set, Dictionary)

Swift๋Š” ๊ตฌ๋ถ„๋˜๋Š” ์ด๋ฆ„์œผ๋กœ ๊ฐ’์„ ์ €์žฅํ•˜๊ณ  ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•ด ๋ณ€์ˆ˜(Variables)๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ, ๊ฐ’์ด ๋ณ€ํ•˜์ง€ ์•Š๋Š” ์ƒ์ˆ˜(Constansts)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ, ์ž‘์—…์„ ํ•  ๋•Œ ๋” ์•ˆ์ „ํ•˜๊ณ  ๋ถ„๋ช…ํ•˜๊ฒŒ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋˜, ํŠœํ”Œ(Tuple)๊ณผ ๊ฐ™์ด ๋” ๋ฐœ์ „๋œ ํƒ€์ž…์„ ๋„์ž…ํ–ˆ์Šต๋‹ˆ๋‹ค.

ํŠœํ”Œ์€ ์—ฌ๋Ÿฌ ๊ฐ’๋“ค์„ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ๋งŒ๋“ค๊ณ , ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ ํ•จ์ˆ˜์—์„œ ๋‹ค์–‘ํ•œ ๊ฐ’๋“ค์„ ๋‹จ์ผ ๋ณตํ•ฉ ๊ฐ’์œผ๋กœ ๋ฆฌํ„ดํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

(โ—๏ธ์—ฌ๋Ÿฌ ๊ฐ’๋“ค์„ ํ•˜๋‚˜์˜ ๋ฐ์ดํ„ฐ ๋ฌถ์Œ์œผ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ง์ž…๋‹ˆ๋‹ค!)

 

Swift๋Š” ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์Œ์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด ์˜ต์…”๋…ˆ ํƒ€์ž…(Opteional Type)์„ ๋„์ž…ํ–ˆ์Šต๋‹ˆ๋‹ค.

์˜ต์…”๋„์€ '๊ฐ’์ด ์žˆ๊ณ , x์™€ ๊ฐ™๋‹ค' ๋˜๋Š” '๊ฐ’์ด ์—†๋‹ค'๋ฅผ ์•Œ๋ ค์ค๋‹ˆ๋‹ค.

 

Swift๋Š” ํƒ€์ž…-์„ธ์ดํ”„ (type-safe)์–ธ์–ด์ž…๋‹ˆ๋‹ค.

์ด๊ฒƒ์€ ์šฐ๋ฆฌ๊ฐ€ ์ž‘์—…ํ•˜๋Š” ์ฝ”๋“œ์—์„œ ๋‹ค๋ฃจ๋Š” ๊ฐ’๋“ค์˜ ํƒ€์ž…์„ ๋ถ„๋ช…ํžˆ ํ•˜๋Š”๋ฐ ๋„์›€์„ ์ค€๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค..

 

๋งŒ์•ฝ, ์ฝ”๋“œ์—์„œ String์„ ์š”๊ตฌํ•œ๋‹ค๋ฉด, ํƒ€์ž… ์„ธ์ดํ”„๋Š” ์šฐ๋ฆฌ๊ฐ€ ์‹ค์ˆ˜๋กœ Intํƒ€์ž…์„ ์ „๋‹ฌํ•˜๋Š” ๊ฒƒ์„ ๋ง‰์•„์ค๋‹ˆ๋‹ค.

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ํƒ€์ž… ์„ธ์ดํ”„๋Š” ์šฐ๋ฆฌ๊ฐ€ ์˜ต์…”๋„์ด ์•„๋‹Œ String์„ ์š”๊ตฌํ•˜๋Š” ์ฝ”๋“œ์— ์˜ต์…”๋„ Stirng์„ ์ „๋‹ฌํ•˜๋ ค๊ณ  ํ•˜๋Š” ๊ฒƒ์„ ๋ง‰์•„์ค๋‹ˆ๋‹ค.

 

ํƒ€์ž… ์„ธ์ดํ”„๋Š” ๊ฐœ๋ฐœ ๋‹จ๊ณ„์—์„œ ๊ฐ€๋Šฅํ•œ ๋น ๋ฅด๊ฒŒ ์—๋Ÿฌ๋ฅผ ์ฐพ๊ณ  ๊ณ ์น  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ค๋‹ˆ๋‹ค.


์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜ (Constants and Variables)

์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜๋Š” ์ด๋ฆ„(maximumNumberOfLoginAttempts ๋˜๋Š” welcomeMessage)๊ณผ

ํŠน์ • ํƒ€์ž…(์ˆซ์ž 10 ๋˜๋Š” ๋ฌธ์ž์—ด "Hello"์™€ ๊ฐ™์€ ํƒ€์ž…)์˜ ๊ฐ’์„ ์—ฐ๊ฒฐํ•ฉ๋‹ˆ๋‹ค.

 

์ƒ์ˆ˜์˜ ๊ฐ’์€ ํ•œ ๋ฒˆ ์„ค์ •๋˜๋ฉด ๋ฐ”๊ฟ€ ์ˆ˜ ์—†์ง€๋งŒ, ๋ฐ˜๋ฉด์— ๋ณ€์ˆ˜๋Š” ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

let maximumNumberOfLoginAttempts = 10
var wellcomeMessage = "Hello

 


์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜ ์„ ์–ธ (Declaring Constants and Variables)

์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜๋Š” ์‚ฌ์šฉ ์ „์— ์„ ์–ธ์ด ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ƒ์ˆ˜๋Š” let ํ‚ค์›Œ๋“œ๋กœ, ๋ณ€์ˆ˜๋Š” var ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

์•„๋ž˜ ์˜ˆ์‹œ๋Š” ์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ์ž ๋กœ๊ทธ์ธ ์‹œ๋„ ํšŸ์ˆ˜๋ฅผ ์ถ”์ ํ•˜๋Š”์ง€๋ฅผ ๋ณด์—ฌ์ค๋‹ˆ๋‹ค.

let maximumNumberOfLoginAttempts = 10
var currentLoginAttempt = 0

 

์ด ์ฝ”๋“œ๋Š” 'maximumNumberOfLoginAttempts'์ธ ์ƒˆ๋กœ์šด ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  10์ด๋ผ๋Š” ๊ฐ’์„ ์ „๋‹ฌํ•˜๊ณ ,

'currentLoginAttempt'์ธ ์ƒˆ๋กœ์šด ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ•˜๊ณ  0์ด๋ผ๋Š” ๊ฐ’์œผ๋กœ ์ „๋‹ฌ ํ•ฉ๋‹ˆ๋‹ค.

 

๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ•  ๋•Œ, ๊ฐ’์„ ์„ ์–ธ๋ถ€์—์„œ ๋„ฃ์–ด์ค„ ์ˆ˜๋„ ์žˆ์ง€๋งŒ,

์•„๋ž˜ 'maximumNumberOfLoginAttemps'์ฒ˜๋Ÿผ ์ดˆ๊ธฐ ๊ฐ’์„ ๋‚˜์ค‘์— ํ• ๋‹นํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

var environment = "development"
let maximumNumberOfLoginAttempts: Int
// maximumNumberOfLoginAttempts has no value yet.

 

 

์ปด๋งˆ(,)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์ƒ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋ฅผ ํ•œ ์ค„์—  ์„ ์–ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

var x = 0.0, y = 0.0, z = 0.0

ํƒ€์ž… ๋ช…์‹œ (Type Annotaions)

๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ type annotaion์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์˜ ํƒ€์ž…์„ ๋ถ„๋ช…ํ•˜๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค.

์ฝœ๋ก (:)์„  ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜ ์ด๋ฆ„ ๋’ค์— ๋‘๊ณ , ํ•œ ์นธ ๋„๊ณ  ์‚ฌ์šฉํ•  ํƒ€์ž…์˜ ์ด๋ฆ„์„ ์“ฐ๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

(โ—๏ธ์ด๊ฑด ์ฝ”๋“œ ๊ทœ์น™์ž…๋‹ˆ๋‹ค)

var welcomeMessage: String

 

์•„๋ž˜ ์ฝ”๋“œ๋Š” ์—๋Ÿฌ์—†์ด ํŠน์ • Stirng ๊ฐ’์„ ๋„ฃ์–ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

(โ—๏ธ์œ„์—์„œ ํƒ€์ž…์„ ๋ช…์‹œํ•ด์ฃผ๊ณ , ๋ณ€์ˆ˜๋ฅผ ์„ ์–ธํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.)

welcomeMessage = "Hello"

 

๊ฐ™์€ ํƒ€์ž…์˜ ์—ฌ๋Ÿฌ ๋ณ€์ˆ˜์˜ ๊ฒฝ์šฐ ์ฝค๋งˆ(,)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•œ ์ค„๋กœ ์„ ์–ธ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

var red, green, blue: Double

 

ํ•˜์ง€๋งŒ, ์‹ค์ „์—์„œ ์šฐ๋ฆฌ๊ฐ€ type annotaion์„ ์‚ฌ์šฉํ•˜๋Š” ์ผ์€ ๊ฑฐ์˜ ์—†์Šต๋‹ˆ๋‹ค.

 

์ดˆ๊ธฐ์— ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜๋ฅผ ์„ ์–ธํ•˜๋ฉด์„œ ์ดˆ๊ธฐ ๊ฐ’์„ ์ „๋‹ฌํ•ด์ค€๋‹ค๋ฉด, Swift๋Š” ํƒ€์ž…์„ ์ถ”๋ก ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ์ด๋Š” ๋’ค์—์„œ ๋” ์ž์„ธํžˆ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์œ„์˜ ์˜ˆ์‹œ์˜ ๊ฒฝ์šฐ ์„ ์–ธ ๋ถ€์—์„œ ์ดˆ๊ธฐ ๊ฐ’์„ ๋„ฃ์–ด์ฃผ์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— type anootation์„ ์‚ฌ์šฉํ•œ ๊ฒ๋‹ˆ๋‹ค.

(โ—๏ธ๋‹ค์‹œ ๋งํ•ด, ์•„๋ž˜์™€ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค๋ฉด type annotation ์—†์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋„ ๋œ๋‹ค๋Š” ๋ง์ž…๋‹ˆ๋‹ค.)

var welcomeMessage = "Hello"

์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„ ์ง“๊ธฐ (Naming Constants and Variables)

์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜ ์ด๋ฆ„์€ ์œ ๋‹ˆ ์ฝ”๋“œ(Unicode)๋ฌธ์ž๋ฅผ ํฌํ•จํ•˜์—ฌ ๋Œ€๋ถ€๋ถ„์˜ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

(โ—๏ธํ•œ์ž, ์ด๋ชจํ‹ฐ์ฝ˜, ํŒŒ์ด ๋“ฑ์„ ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)

let π = 3.14159
let ไฝ ๅฅฝ = "ไฝ ๅฅฝไธ–็•Œ"
let ๐Ÿถ๐Ÿฎ = "dogcow"

 

์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜ ์ด๋ฆ„์€ ๊ณต๋ฐฑ, ์ˆ˜ํ•™์  ๊ธฐํ˜ธ, ํ™”์‚ดํ‘œ, ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์œ ๋‹ˆ์ฝ”๋“œ ์Šค์นผ๋ผ ๊ฐ’, ๋˜๋Š” ์„ ๊ณผ ๋ฐ•์Šค๋ฅผ ๊ทธ๋ฆฌ๋Š” ๋ฌธ์ž๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

์ˆซ์ž๋Š” ์ด๋ฆ„์˜ ๋‹ค๋ฅธ ๊ณณ์—๋Š” ํฌํ•จ๋  ์ˆ˜ ์žˆ์ง€๋งŒ ์ˆซ์ž๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ด๋ฆ„์„ ์„ ์–ธํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค.

 

Swift ํ‚ค์›Œ๋“œ์™€ ๋™์ผํ•œ ์ด๋ฆ„์˜ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด, ๋ฐฑํ‹ฑ(`)์œผ๋กœ ๋ฌถ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์„ ํƒ์˜ ์—ฌ์ง€๊ฐ€ ์—†์„ ๋•Œ๊นŒ์ง€๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋ฉ๋‹ˆ๋‹ค.

(โ—๏ธSwift์—์„œ ์ž์ฒด์ ์œผ๋กœ ์ •ํ•ด๋‘” ํ‚ค์›Œ๋“œ(์˜ˆ๋ฅผ ๋“ค์–ด var, let)์„ ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜ ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์ ์–ด์•ผ ๋ฉ๋‹ˆ๋‹ค.)

(โ—๏ธํ˜น์‹œ๋‚˜ ๋ฐฑํ‹ฑ์ด ๋ญ”์ง€ ๋ชจ๋ฅด์‹œ๋Š” ๋ถ„๋“ค์„ ์œ„ํ•ด.. ํ‚ค๋ณด๋“œ์—์„œ โ‚ฉ๋ฅผ ์ฐพ์œผ์‹  ๋’ค, ์˜๋ฌธ ํ‚ค๋กœ ๋ฐ”๊ฟ”์„œ ํ•ด๋‹น ํ‚ค๋ฅผ ๋ˆ„๋ฅด๋ฉด ๋ฐฑํ‹ฑ(`)์ด ๋‚˜์˜ต๋‹ˆ๋‹ค!)

var `var` = "var"
let `let` = "let"

print(`var`)
print(`let`)

/* - ์ถœ๋ ฅ
var
let
*/

 

๋ณ€์ˆ˜๋Š” ์ดˆ๊ธฐ๊ฐ’์„ ๊ฐ™์€ ํƒ€์ž…์˜ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์ง€๋งŒ, ์ƒ์ˆ˜๋Š” ๋ฐ”๊พธ๋ ค๊ณ  ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

var friendlyWelcome = "Hello!"
friendlyWelcome = "Bonjour!"
// friendlyWelcome is now "Bonjour!"

let languageName = "Swift"
languageName = "Swift++"
// This is a compile-time error: languageName cannot be changed.

๋ณ€์ˆ˜์™€ ์ƒ์ˆ˜ ํ”„๋ฆฐํŠธํ•˜๊ธฐ (Printing Constants and Variables)

์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜์˜ ํ˜„์žฌ ๊ฐ’์„ print(_:separator:terminator:) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋ฆฐํŠธ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

print(friendlyWelcome)
// Prints "Bonjour!"

 

print ํ•จ์ˆ˜๋Š” Xcode์˜ "console"์ฐฝ์— ํ•˜๋‚˜ ์ด์ƒ์˜ ๊ฒฐ๊ณผ๋ฅผ ํ”„๋ฆฐํŠธ ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

 

separator์™€ terminator๋Š” default ๊ฐ’์ด ์žˆ์–ด ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ๊ฐ’์„ ๋„ฃ์ง€ ์•Š์•„๋„ ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.

terminator์˜ default ๊ฐ’์œผ๋กœ ์ธํ•ด, ํ”„๋ฆฐํŠธ๊ฐ€ ๋๋‚˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์ค„ ๋ฐ”๊ฟˆ์ด ๋ฉ๋‹ˆ๋‹ค.

์ด๋ฅผ ๋ฐ”๊พธ๊ณ  ์‹ถ๋‹ค๋ฉด ํŒŒ๋ผ๋ฏธํ„ฐ์— ์ง์ ‘ ๊ฐ’์„ ๋„ฃ์–ด์„œ ์‹คํ–‰ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. 

print("Hello", terminator: " ") // ํ•œ ์นธ ๋„์–ด์“ฐ๊ธฐ๋งŒ ํ•˜๊ณ  print๋ฌธ์„ ์ข…๋ฃŒํ•œ๋‹ค.
print("Leo")

/* - ์ถœ๋ ฅ
Hello Leo
*/

 

Swift๋Š” ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์˜ ์ด๋ฆ„์„ ๊ฐ’์œผ๋กœ ๋Œ€์ฒดํ•˜์—ฌ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ๋ฌธ์ž์—ด ๋ณด๊ฐ„๋ฒ•(Stirng Interploation)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

(โ—๏ธ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์˜ ์ด๋ฆ„์„ \()์˜ ๊ด„ํ˜ธ ์‚ฌ์ด์— ์ž‘์„ฑํ•˜๋ฉด, ํ•ด๋‹น ์ž๋ฆฌ์— ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜ ์ด๋ฆ„์— ์ €์žฅ๋˜์–ด ์žˆ๋Š” ๊ฐ’์œผ๋กœ ๋Œ€์ฒด๋˜์–ด ํ”„๋ฆฐํŠธ๋ฉ๋‹ˆ๋‹ค.)

print("The current value of friendlyWelcome is \(friendlyWelcome)")
// Prints "The current value of friendlyWelcome is Bonjour!"

์ฃผ์„ (Comments)

์ฝ”๋“œ์—์„œ ์‹คํ–‰๋˜์ง€ ์•Š๋Š” ํ…์ŠคํŠธ๋ฅผ ํฌํ•จํ•˜๊ธฐ ์œ„ํ•ด ์ฃผ์„์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ๊ฐ€ ์ปดํŒŒ์ผ๋  ๋•Œ, ์ฃผ์„์€ ๋ฌด์‹œ๋ฉ๋‹ˆ๋‹ค.

 

ํ•œ ์ค„ ์ฃผ์„์€ //์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

// This is a comment.

 

์—ฌ๋Ÿฌ ์ค„์˜ ์ฃผ์„์€ \*, *\ ์‚ฌ์ด์— ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

/* This is also a comment
but is written over multiple lines. */

 

C์–ธ์–ด์™€๋Š” ๋‹ค๋ฅด๊ฒŒ, ์ฃผ์„ ์•ˆ์— ๋‹ค๋ฅธ ์ฃผ์„์„ ์ค‘์ฒฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

/* This is the start of the first multiline comment.
    /* This is the second, nested multiline comment. */
This is the end of the first multiline comment. */

(โ—๏ธ์‚ฌ์‹ค ์ด๊ฑธ ์–ธ์ œ ์“ธ๊นŒ ์‹ถ์—ˆ์ง€๋งŒ, ์˜ˆ๋ฅผ ๋“ค์–ด, ํ•จ์ˆ˜ ๋‚ด๋ถ€ ์ค‘๊ฐ„ ์ฏค ์ฃผ์„์œผ๋กœ ์ฝ”๋“œ ์„ค๋ช…์ด ์จ์žˆ๋Š”๋ฐ, ์ฃผ์„ ์•ž ๋’ค๋กœ ์ž ๊น ๋‹ค๋ฅธ ์ฝ”๋“œ๋“ค์„ ์ฃผ์„ ์ฒ˜๋ฆฌ๋ฅผ ํ•˜๊ณ  ์‹ถ์„๋•Œ ์•„์ฃผ ์œ ์šฉํ•˜๊ฒŒ ํ•œ ๋ฒˆ์— ๋“œ๋ž˜๊ทธํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒ๋‹ˆ๋‹ค!!)

 

(โ—๏ธ ์ถ”๊ฐ€ ๊ฟ€ํŒ, ์ฃผ์„ ์ฒ˜๋ฆฌํ•˜๊ณ  ์‹ถ์€ ์ฝ”๋“œ๋ฅผ ํ•œ ๋ฒˆ์— ๋“œ๋ž˜๊ทธ ํ•˜๊ณ  cmd +/ ๋ˆ„๋ฅด๋ฉด ํ•œ ๋ฒˆ์— ์ฃผ์„ ์ฒ˜๋ฆฌ ๋ฉ๋‹ˆ๋‹ค~)


์„ธ๋ฏธํด๋ก  (Semicolons)

๋‹ค๋ฅธ ์–ธ์–ด์™€ ๋‹ฌ๋ฆฌ, Swift๋Š” ์ฝ”๋“œ ๋ฌธ์žฅ ๋งˆ์ง€๋ง‰์— ์„ธ๋ฏธํด๋ก (;)์„ ์š”๊ตฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ ํ•œ ์ค„์— ์—ฌ๋Ÿฌ ์‹คํ–‰๋ฌธ์„ ์“ฐ๊ณ  ์‹ถ๋‹ค๋ฉด ์„ธ๋ฏธํด๋ก ์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

let cat = "๐Ÿฑ"; print(cat)
// Prints "๐Ÿฑ"

์ •์ˆ˜ (Integers)

์ •์ˆ˜๋Š” 42์™€ -23๊ณผ ๊ฐ™์ด ๋ถ„์ˆ˜๊ฐ€ ์•„๋‹Œ ์ „์ฒด ์ˆซ์ž์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ, ๋ถ€ํ˜ธ๊ฐ€ ์žˆ๋Š” ์ •์ˆ˜ (signed) (์–‘์ˆ˜, 0, ๋˜๋Š” ์Œ์ˆ˜) ๋˜๋Š” ๋ถ€ํ˜ธ๊ฐ€ ์—†๋Š” ์ •์ˆ˜ (unsigned) (์–‘์ˆ˜ ๋˜๋Š” 0)์ด ์žˆ์Šต๋‹ˆ๋‹ค.

Swift๋Š” 8, 16, 32, 64๋น„ํŠธ ํ˜•์‹์˜ ์ •์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๊ณ , C์™€ ์œ ์‚ฌํ•˜๊ฒŒ 8๋น„ํŠธ์˜ ๋ถ€ํ˜ธ ์—†๋Š” ์ •์ˆ˜๋Š” UInt8, 32๋น„ํŠธ์˜ ๋ถ€ํ˜ธ ์žˆ๋Š” ์ •์ˆ˜๋Š” Int32๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค. Swift์˜ ๋ชจ๋“  ํƒ€์ž…๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ •์ˆ˜ ํƒ€์ž…๋„ ๋Œ€๋ฌธ์ž๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

(โ—๏ธ๋ถ€ํ˜ธ๊ฐ€ ์—†๋Š” ์ •์ˆ˜๋Š” UInt๋กœ ํ‘œ์‹œ, ๋ถ€ํ˜ธ๊ฐ€ ์žˆ๋‹ค๋ฉด Int๋กœ ํ‘œ์‹œํ•˜๊ณ , ๋’ค์— ๋น„ํŠธ ์ˆ˜๋ฅผ ๋ถ™์—ฌ์ฃผ๋ฉด ๋ฉ๋‹ˆ๋‹ค.)


์ •์ˆ˜ ๋ฒ”์œ„ (Integer Bounds)

๊ฐ ์ •์ˆ˜ ํƒ€์ž…์˜ max, minํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ตœ์†Œ ๊ฐ’๊ณผ ์ตœ๋Œ€ ๊ฐ’์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let minValue = UInt8.min  // minValue is equal to 0, and is of type UInt8
let maxValue = UInt8.max  // maxValue is equal to 255, and is of type UInt8

Int

๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ, ์šฐ๋ฆฌ๊ฐ€ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ •์ˆ˜์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ๊ตฌ์ฒด์ ์œผ๋กœ ์ •ํ•  ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค. 

Swift์—์„œ ํ˜„์žฌ ๋„ค์ดํ‹ฐ๋ธŒ ํฌ๊ธฐ์™€ ๊ฐ™์€ ์‚ฌ์˜์ฆˆ์˜ Intํƒ€์ž…์„ ์ œ๊ณตํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

  • 32-bit ํ”Œ๋žซํผ์—์„œ Int ๋Š” Int32 ์™€ ๊ฐ™์€ ํฌ๊ธฐ๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค.
  • 64-bit ํ”Œ๋žซํผ์—์„œ Int ๋Š” Int64 ์™€ ๊ฐ™์€ ํฌ๊ธฐ๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค.

์šฐ๋ฆฌ๋Š” ๊ตฌ์ฒด์ ์ธ ์‚ฌ์ด์ฆˆ์˜ ์ •์ˆ˜๋กœ ์ž‘์—…์„ํ•ด์•ผํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด, ๊ทธ๋ƒฅ Intํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

32-bitํ”Œ๋žซํผ์—์„œ ์‚ฌ์šฉ๋˜๋Š” Int(Int32)๋Š” -2,147,483,648 ~ 2,147,483,647 ์‚ฌ์ด์˜ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ถฉ๋ถ„ํžˆ ํฐ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

(โ—๏ธ์™œ 32๋น„ํŠธ๊ฐ€ ์ €๋Ÿฐ ์ˆซ์ž๋ฅผ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๋Š” ์ง€ ๊ถ๊ธˆํ•˜์‹œ๋‹ค๋ฉด, ๋ฉ”๋ชจ๋ฆฌ์™€ ์ด์ง„๋ฒ•์— ๋Œ€ํ•ด์„œ ์ฐพ์•„๋ณด์„ธ์š”!!)


UInt

Swift๋Š” ๋ถ€ํ˜ธ๊ฐ€ ์—†๋Š” ์ •์ˆ˜, UInt ํƒ€์ž…์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์œ„์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ”Œ๋žซํผ์— ๋”ฐ๋ผ ์•Œ์•„์„œ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ •ํ•ด์ง‘๋‹ˆ๋‹ค.

 

โญ๏ธ UInt๊ฐ€ ํŠน๋ณ„ํ•˜๊ฒŒ ํ•„์š”ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด, ์–‘์ˆ˜๋กœ ์•Œ๋ ค์ง„ ๊ฐ’์ด ์ €์žฅ๋˜๋”๋ผ๋„ Int๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

Int ํƒ€์ž…์˜ ์ผ๊ด€๋œ ์‚ฌ์šฉ์€ ์ฝ”๋“œ ์ƒํ˜ธ ์šด์šฉ์„ฑ(interoperability)๋ฅผ ์ง€์›ํ•˜๊ณ , ๋‹ค๋ฅธ ์ˆซ์ž ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

 

(โ—๏ธ์ƒํ˜ธ ์šด์šฉ์„ฑ์€ ํŠน๋ณ„ํ•œ ๋…ธ๋ ฅ ์—†์ด๋„ ์ƒํ˜ธ ๊ฐ„์˜ ๊ตํ™˜์„ ํšจ๊ณผ์ ์œผ๋กœ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋Šฅ๋ ฅ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด, ๋Œ€๋ถ€๋ถ„์ด Int ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ํ˜ผ์ž Int8 ํƒ€์ž…์„ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด ๋‹ค๋ฅธ ์‚ฌ๋žŒ๊ณผ์˜ ์›ํ™œํ•œ ํ˜„์—…์„ ํ•˜์ง€ ๋ชปํ•˜๋ฉฐ, ์ด๋Š” ์ƒํ˜ธ ์šด์šฉ์„ฑ์ด ๊นจ์กŒ๋‹ค๊ณ  ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.)

 

(โ—๏ธ๋” ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•˜์ž๋ฉด, ๋Œ€๋ถ€๋ถ„์ด Int ์“ฐ๋‹ˆ๊นŒ ๋„ˆ๋„ Int ์จ! ๋ผ๊ณ  ๋งํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.)


๋ถ€๋™ ์†Œ์ˆ˜์   ์ˆซ์ž(Floating-Point Numbers)

๋ถ€๋™ ์†Œ์ˆ˜์  ์ˆซ์ž๋Š” 3.141592, 0.1, -231.13์™€ ๊ฐ™์€ ๋ถ„์ˆ˜ ์„ฑ๋ถ„์„ ๊ฐ–๋Š” ์ˆซ์ž์ž…๋‹ˆ๋‹ค.

Int ํƒ€์ž…์˜ ๊ฐ’๋ณด๋‹ค ๋” ๋„“์€ ๋ฒ”์˜์˜ ๊ฐ’์„ ํ‘œํ˜„ํ•  ์ˆ˜ ์žˆ๊ณ , ๋” ํฌ๊ณ  ์ž‘์€ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

Swift๋Š” Double๊ณผ Float์˜ ๋‘ ๊ฐ€์ง€ ๋ถ€๋™ ์†Œ์ˆ˜์  ํƒ€์ž…์„ ์ œ๊ณตํ•˜๊ณ ,

 

Double์€ 64๋น„ํŠธ์˜ ๋ถ€๋™ ์†Œ์ˆ˜์  ์ˆซ์ž, Float๋Š” 32๋น„ํŠธ์˜ ๋ถ€๋™ ์†Œ์ˆ˜์  ์ˆซ์ž๋ฅผ ํ‘œํ˜„ํ•ฉ๋‹ˆ๋‹ค.

Double ํƒ€์ž…์€ ์ตœ์†Œ 15์ž๋ฆฌ์˜ ์†Œ์ˆ˜์  ์ •ํ™•๋„๋ฅผ ๊ฐ–๊ณ , Float ํƒ€์ž…์€ ์ตœ์†Œ 6์ž๋ฆฌ์˜ ์†Œ์ˆ˜์  ์ •ํ™•๋„๋ฅผ ๊ฐ–์Šต๋‹ˆ๋‹ค.

var b: Float = 3.123456//์‹ค์ˆ˜, ๋ถ€๋™์†Œ์ˆ˜(6์ž๋ฆฌ):
var c: Double = 3.123456789012345//์‹ค์ˆ˜, ๋ถ€๋™์†Œ์ˆ˜(15์ž๋ฆฌ)

print(b)
print(c)

 

(โ—๏ธ๋” ์“ฐ๋ฉด ์–ด๋–ป๊ฒŒ ๋‚˜์˜ค๋‚˜ ๊ถ๊ธˆํ•ด์„œ, playground์—์„œ ์ง์ ‘ ํ”„๋ฆฐํŠธ ํ•ด๋ดค๋Š”๋ฐ..

์ € ์ƒํƒœ์—์„œ ๋’ค์— ์ˆซ์ž๋ฅผ ๋” ์“ด ๋’ค ํ”„๋ฆฐํŠธ๋ฅผ ํ•˜๋ฉด, ์†Œ์ˆ˜์  ๋’ค๋กœ 7, 16์ž๋ฆฌ๊นŒ์ง€ ๋ณด์ด์ง€๋งŒ, ๋งจ ๋’ค์— ์ˆซ์ž๋Š” ์ •ํ™•ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค!)

 

์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‘ ๊ฐœ์˜ ๋ถ€๋™ ์†Œ์ˆ˜์  ํƒ€์ž… ์ค‘ ์ ์ ˆํ•œ ๊ฒƒ์„ ๊ณจ๋ผ์•ผ ํ•˜๊ฒ ์ง€๋งŒ, ๋‘˜ ๋‹ค ๊ดœ์ฐฎ๋‹ค๋ฉด Double์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.


ํƒ€์ž… ์„ธ์ดํ”„ํ‹ฐ์™€ ํƒ€์ž… ์ถ”๋ก  (Type Safety and Type Inference)

Swift๋Š” ํƒ€์ž… ์„ธ์ดํ”„(type-safe) ์–ธ์–ด์ž…๋‹ˆ๋‹ค. ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผ ํ•  ๋•Œ ํƒ€์ž… ๊ฒ€์‚ฌ๋ฅผ ํ•˜๊ณ  ์ผ์น˜ํ•˜์ง€ ์•Š๋Š” ํƒ€์ž…์€ ์˜ค๋ฅ˜๋กœ ํ‘œ์‹œ๋ฅผ ํ•ด์ค๋‹ˆ๋‹ค.

์ด๋ฅผ ํ†ตํ•ด ๊ฐœ๋ฐœ ๋‹จ๊ณ„์—์„œ ๊ฐ€๋Šฅํ•œ ๋นจ๋ฆฌ ์˜ค๋ฅ˜๋ฅผ ์ฐพ๊ณ  ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜ ์ด๊ฒƒ์ด ์„ ์–ธํ•˜๋Š” ๋ชจ๋“  ์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜์˜ ํƒ€์ž…์„ type-annotaion์œผ๋กœ ์ง€์ •ํ•ด์ค˜์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์€ ์•„๋‹™๋‹ˆ๋‹ค. 

Swift๋Š” ํ•„์š”ํ•œ ๊ฐ’์˜ ํŠน์ • ํƒ€์ž…์ด ์ง€์ •๋˜์–ด ์žˆ์ง€ ์•Š๋‹ค๋ฉด ์ ์ ˆํ•œ ํƒ€์ž… ์ถ”๋ก  (Type inference)์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

ํƒ€์ž… ์ถ”๋ก ์„ ํ†ตํ•ด ์ฝ”๋“œ๋ฅผ ์ปดํŒŒ์ผ ํ•  ๋•Œ ์ œ๊ณตํ•œ ๊ฐ’์„ ๊ฒ€์‚ฌํ•˜์—ฌ ํŠน์ • ์‹์˜ ํƒ€์ž…์„ ์ž๋™์œผ๋กœ ์ถ”๋ก ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

๋”ฐ๋ผ์„œ Swift๋Š” C๋‚˜ Objective-C์™€ ๊ฐ™์€ ์–ธ์–ด๋ณด๋‹ค ํƒ€์ž… ์„ ์–ธ์„ ๋” ์ ๊ฒŒ ์š”๊ตฌํ•ฉ๋‹ˆ๋‹ค.

 

์˜ˆ๋ฅผ ๋“ค์–ด, 42์˜ ๊ฐ’์„ ํƒ€์ž… ์„ ์–ธ ์—†์ด ๋ฆฌํ„ฐ๋Ÿด ๊ฐ’๋งŒ ํ• ๋‹นํ•ด์ค€๋‹ค๋ฉด Swift๋Š” type infernce๋ฅผ ํ†ตํ•ด Intํ˜•์œผ๋กœ ์ถ”๋ก ์„ ํ•ฉ๋‹ˆ๋‹ค.

let meaningOfLife = 42
// meaningOfLife is inferred to be of type Int

 

๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ํŠน๋ณ„ํ•˜๊ฒŒ ํƒ€์ž… ๋ช…์‹œ๊ฐ€ ์—†์ง€๋งŒ, ์•Œ์•„์„œ Double๋กœ ์ถ”๋ก ํ•ฉ๋‹ˆ๋‹ค.

(โ—๏ธ Float ํƒ€์ž…์œผ๋กœ ์ถ”๋ก ํ•  ์ˆ˜๋„ ์žˆ๊ฒ ์ง€๋งŒ, ์•„๊นŒ ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ ๊ฒƒ์ฒ˜๋Ÿผ Swift๋„ ์ƒํ˜ธ ์šด์šฉ์„ฑ์„ ์œ„ํ•ด Double ํƒ€์ž…์„ ์ฆ๊ฒจ ์“ฐ๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.)

let pi = 3.14159
// pi is inferred to be of type Double

 

์ •์ˆ˜์™€ ๋ถ€๋™ ์†Œ์ˆ˜์ ์„ ๋”ํ•˜๋Š” ํ‘œํ˜„์‹์„ ์จ๋„, ๋’ค์— ๋ถ€๋™ ์†Œ์ˆ˜์ ์„ ๋ณด๊ณ  Double ํƒ€์ž…์œผ๋กœ ์ถ”๋ก ํ•ฉ๋‹ˆ๋‹ค.

๋‹ค์‹œ ๋งํ•ด, ํ‘œํ˜„์‹์˜ ๊ฒฐ๊ณผ (output)์„ ๋ณด๊ณ  ํƒ€์ž…์„ ์ถ”๋ก ํ•ฉ๋‹ˆ๋‹ค.

let anotherPi = 3 + 0.14159
// anotherPi is also inferred to be of type Double

์ˆซ์ž ๋ฆฌํ„ฐ๋Ÿด (Numeric Literals)

์ •์ˆ˜ ๋ฆฌํ„ฐ๋Ÿด์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์“ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

  • 0์ง„์ˆ˜๋Š” ์ ‘๋‘์‚ฌ ์—†์ด ํ‘œํ˜„
  • 2์ง„์ˆ˜๋Š” 0b๋ฅผ ์ ‘๋‘์‚ฌ๋กœ ํ‘œํ˜„ (b = binary)
  • 8์ง„์ˆ˜ 0o๋ฅผ ์ ‘๋‘์‚ฌ๋กœ ํ‘œํ˜„ (o = octal)
  • 16์ง„์ˆ˜ 0x๋ฅผ ์ ‘๋‘์‚ฌ๋กœ ํ‘œํ˜„ (x = hexadecimal)
let decimalInteger = 17
let binaryInteger = 0b10001       // 17 ์„ 2์ง„์ˆ˜๋กœ ํ‘œํ˜„
let octalInteger = 0o21           // 17 ์„ 8์ง„์ˆ˜๋กœ ํ‘œํ˜„
let hexadecimalInteger = 0x11     // 17 ์„ 16์ง„์ˆ˜๋กœ ํ‘œํ˜„

๋ถ€๋™ ์†Œ์ˆ˜์  ๋ฆฌํ„ฐ๋Ÿด์€ ์•„๋ž˜์™€ ๊ฐ™์ด ํ‘œํ˜„๋ฉ๋‹ˆ๋‹ค.

  • 10์ง„์ˆ˜๋Š” ์ ‘๋‘์‚ฌ ์—†์ด ํ‘œํ˜„
    • e,E๋กœ ์ง€์ˆ˜๋ฅผ ํ‘œ์‹œ (์ง€์ˆ˜๊ฐ€ x์ธ 10์ง„์ˆ˜๋Š” 10^๊ฐ€ ๊ณฑํ•ด์ง)
    • 1.25e2 = 1.25 x 10²
  • 16์ง„์ˆ˜๋Š”  0x๋ฅผ ์ ‘๋‘์‚ฌ๋กœ ํ‘œํ˜„
    • p, P๋กœ ์ง€์ˆ˜๋ฅผ ํ‘œ์‹œ  (์ง€์ˆ˜๊ฐ€ x์ธ 16์ง„์ˆ˜๋Š” 2^๊ฐ€ ๊ณฑํ•ด์ง)
    • 0xFp2 = 15 x 2²

์•„๋ž˜๋Š” ๋ชจ๋‘ 12.1875๋ฅผ ํ‘œํ˜„ํ•˜๋Š” ๋ฆฌํ„ฐ๋Ÿด์ž…๋‹ˆ๋‹ค.

let decimalDouble = 12.1875
let exponentDouble = 1.21875e1
let hexadecimalDouble = 0xC.3p0

 

์ˆซ์ž ๋ฆฌํ„ฐ๋Ÿด์„ ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€ ํฌ๋งท์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ •์ˆ˜์™€ ๋ถ€๋™ ์†Œ์ˆ˜์  ๋ชจ๋‘ ์ถ”๊ฐ€์ ์ธ 0์œผ๋กœ ์ฑ„์›Œ์งˆ ์ˆ˜ ์žˆ๊ณ , ๊ฐ€๋…์„ฑ์„ ์œ„ํ•œ ๋ฐ‘์ค„์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_000_1

 

(โ—๏ธ์ด ๋ถ€๋ถ„์€ ์™ธ์šด๋‹ค๊ธฐ๋ณด๋‹ค.. ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ๋‹ค์‹œ ๋ด์•ผํ•  ๋“ฏ ํ•ฉ๋‹ˆ๋‹ค ๐Ÿ˜…)


์ˆซ์ž ํƒ€์ž… ๋ณ€ํ™˜ (Numeric Type Conversion)

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

 

์™ธ๋ถ€ ์†Œ์Šค์—์„œ ํŠน๋ณ„ํžˆ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ง€์ •๋œ ๋ฐ์ดํ„ฐ๋‚˜ ์„ ์€, ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ, ๋˜๋Š” ์ตœ์ ํ™”๊ฐ€ ํ•„์š”ํ•œ ํŠน๋ณ„ํ•œ ๊ฒฝ์šฐ์—๋งŒ ๋‹ค๋ฅธ ์ •์ˆ˜ ํƒ€์ž…์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ง€์ •๋œ ํƒ€์ž…์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํŠน์ • ์‚ฌ๊ฑด์œผ๋กœ ์ธํ•œ ๊ฐ’์˜ ์˜ค๋ฒ„ํ”Œ๋กœ์šฐ๋ฅผ ํฌ์ฐฉํ•˜๊ณ , ์‚ฌ์šฉ์ค‘์ธ ๋ฐ์ดํ„ฐ์˜ ํŠน์„ฑ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์ •์ˆ˜ ๋ณ€ํ™˜ (Integer Conversion)

์ •์ˆ˜๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜์˜ ์ˆซ์ž ๋ฒ”์œ„๋Š” ๊ฐ ์ˆซ์ž ํƒ€์ž…์— ๋”ฐ๋ผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

Int8 ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๋Š” -128 ๊ณผ 127 ์‚ฌ์ด์˜ ์ˆซ์ž๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, UInt8 ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๋Š” 0 ๊ณผ 255 ์‚ฌ์ด์˜ ์ˆซ์ž๋ฅผ ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํฌ๊ธฐ๊ฐ€ ์ง€์ •๋œ ์ •์ˆ˜ ํƒ€์ž…์˜ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜์— ๋งž์ง€์•Š๋Š” ์ˆซ์ž๋Š” ์ปดํŒŒ์ผ ์‹œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

let cannotBeNegative: UInt8 = -1
// UInt8 can't store negative numbers, and so this will report an error
let tooBig: Int8 = Int8.max + 1
// Int8 can't store a number larger than its maximum value,
// and so this will also report an error

 

๊ฐ ์ˆซ์ž ํƒ€์ž…์€ ๋‹ค๋ฅธ ๋ฒ”์œ„์˜ ๊ฐ’์„ ์ €์žฅํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ์ˆซ์ž ํƒ€์ž… ๋ณ€ํ™˜์„ ๊ฐ ํƒ€์ž… ๋ณ„๋กœ ์ง„ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ด๋Š” ์ˆจ๊ฒจ์ง„ ํƒ€์ž… ๋ณ€ํ™˜ ์—๋Ÿฌ๋ฅผ ๋ง‰๊ณ  ํƒ€์ž… ์ „ํ™˜ ์˜๋„๋ฅผ ๋ช…์‹œํ•˜๋Š”๋ฐ ๋„์›€์„ ์ค๋‹ˆ๋‹ค.

 

ํŠน์ • ์ˆซ์ž ํƒ€์ž…์„ ๋‹ค๋ฅธ ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋ ค๋จผ ์›ํ•˜๋Š” ํƒ€์ž…์˜ ์ƒˆ ์ˆซ์ž๋กœ ์ดˆ๊ธฐํ™” ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

(โ—๏ธ ๊ธฐ์กด์— ์žˆ๋˜ ์ˆซ์ž์˜ ํƒ€์ž…์„ change ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ, ์™„์ „ํžˆ ์ƒˆ๋กœ์šด ์ˆซ์ž๋ฅผ ๋งŒ๋“ค์–ด ๋‚ธ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค!)

 

UInt16 ํƒ€์ž…๊ณผ UInt8 ํƒ€์ž…์˜ ๊ฐ’๋“ค์€ ํ•จ๊ป˜ ๋”ํ•ด์งˆ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ UInt8์˜ ๊ฐ’์ธ 1์„ UInt16 ํƒ€์ž…์œผ๋กœ ์ƒˆ๋กญ๊ฒŒ ์ดˆ๊ธฐํ™” ํ•ด์„œ ๊ณ„์‚ฐ์„ ์ง„ํ–‰ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ณ„์‚ฐ์˜ ๊ฒฐ๊ณผ์ธ twoThousandAndOne ์ƒ์ˆ˜๋„ ํƒ€์ž… ์ถ”๋ก ์— ์˜ํ•ด UInt16 ํƒ€์ž…์ด ๋ฉ๋‹ˆ๋‹ค.

let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one)

 

SomeType(ofInitialValue)๋Š” Swift ํƒ€์ž…์˜ ์ƒ์„ฑ์ž๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์ดˆ๊ธฐ ๊ฐ’์„ ์ „๋‹ฌํ•ด์ฃผ๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค. 

(โ—๏ธ๋‹ค์‹œ ๋งํ•ด, ํŠน์ • ํƒ€์ž…์˜ ๊ฐ’์„ ์ƒ์„ฑ์ž์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „๋‹ฌ ํ•ด์ฃผ๋ฉด ํ•ด๋‹น ํƒ€์ž…์˜ ์ƒ์„ฑ์ž๊ฐ€ ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ƒ์„ฑํ•ด๋‚ด๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.)

 

any type์€ ์ „๋‹ฌํ•  ์ˆ˜ ์—†์ง€๋งŒ, ํ™•์žฅ์„ ํ†ตํ•ด ๊ตฌํ˜„ํ•ด์ค€๋‹ค๋ฉด ์ปค์Šคํ…€ ํƒ€์ž…์œผ๋กœ๋„ ํ•ด๋‹น ํƒ€์ž…์˜ ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

(โ—๏ธ์ด ๋‚ด์šฉ์€ ๋’ค์— ๋‚ด์šฉ์„ ์•Œ๋ฉด ์ดํ•ด๊ฐ€ ๋˜์‹ค๊ฑฐ์—์š”.)


์ •์ˆ˜์™€ ๋ถ€๋™ ์†Œ์ˆ˜์  ๋ณ€ํ™˜ (Integer and Floating-Point Conversion)

์ •์ˆ˜์™€ ๋ถ€๋™ ์†Œ์ˆ˜์  ์ˆซ์ž ํƒ€์ž…์˜ ๋ณ€ํ™˜์€ ๋ช…์‹œ์ ์œผ๋กœ ์ž‘์„ฑ๋˜์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์ƒ์ˆ˜ three๋Š” Double ํƒ€์ž…๊ณผ ๊ณ„์‚ฐ๋  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, Double(three)๋กœ ํƒ€์ž… ๋ณ€ํ™˜์„ ํ•ด์ฃผ๊ณ  ๊ณ„์‚ฐ์„ ํ•ด์ค๋‹ˆ๋‹ค.

let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneFourOneFiveNine
// pi๋Š” 3.14159์ด๊ณ , Double ํƒ€์ž…์œผ๋กœ ์ถ”๋ก ์ด ๋ฉ๋‹ˆ๋‹ค.

 

๋ถ€๋™ ์†Œ์ˆ˜์ ์˜ ๊ฐ’์ด ์ •์ˆ˜๋กœ ๋ณ€ํ™˜์ด ๋  ๋•Œ ์†Œ์ˆ˜์  ์•„๋ž˜๋ฅผ ๋ฒ„๋ฆผํ•ฉ๋‹ˆ๋‹ค. 

์ด๊ฒƒ์€ 4.75๋Š” 4, ๊ทธ๋ฆฌ๊ณ  -3.9๋Š” -3์ด ๋œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค.

let integerPi = Int(pi)
// ์ •์ˆ˜๋กœ ๋ณ€ํ™˜๋˜๋ฉด์„œ 0.14159๋ฅผ ๋ฒ„๋ฆผํ•ฉ๋‹ˆ๋‹ค.

 

์ˆซ์ž ์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ๊ทœ์น™ (๊ฐ™์€ ํƒ€์ž…๋ผ๋งŒ ๊ณ„์‚ฐ ๊ฐ€๋Šฅ)์€ ์ˆซ์ž ๋ฆฌํ„ฐ๋Ÿด ๊ทœ์น™๊ณผ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

(โ—๏ธ์•„๋ž˜ ์˜ˆ์‹œ๋ฅผ ๋ณด๋ฉด ๋ฌด์Šจ ๋ง์ธ์ง€ ์ดํ•ด๋˜์‹ค๊ฒ๋‹ˆ๋‹ค.)

 

์•„๋ž˜์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋Š”, ์ด๋ฏธ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ 3๊ณผ 3.141592 ๋ฆฌํ„ฐ๋Ÿด์„ ํ‰๊ฐ€ํ•˜๊ณ  ํ•ด๋‹น ์ƒ์ˆ˜์— ํƒ€์ž…์„ ์ถ”๋ก ํ•œ ๋’ค,

๋‹ค๋ฅธ ํƒ€์ž…์˜ ์ƒ์ˆ˜๋ผ๋ฆฌ ๊ฒฐํ•ฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๊ทœ์น™์— ์œ„๋ฐฐ๊ฐ€ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

let three = 3
let pi = 3.141592

let num = three + pi 
//Error: Binary operator '+' cannot be applied to operands of type 'Int' and 'Double'

 

๊ทธ๋Ÿฌ๋‚˜, ์•„๋ž˜ ๊ฒฝ์šฐ๋Š” 3์€ ์ˆซ์ž ๋ฆฌํ„ฐ๋Ÿด์— ๋ช…์‹œ์ ์ธ ํƒ€์ž…์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ๋ฆฌํ„ฐ๋Ÿด ๊ฐ’ 0.14159์— ์ง์ ‘ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๊ฒƒ์˜ ํƒ€์ž…์€ ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ์‹คํ–‰๋˜๋Š” ์‹œ์ ์—์„œ๋งŒ ์ถ”๋ก ๋ฉ๋‹ˆ๋‹ค.

(โ—๏ธ์•„๋งˆ.. 3์„ ์ถ”๋ก ํ•˜๋ ค๊ณ  ๋ณด๋‹ˆ ๋’ค์— Double ํƒ€์ž…๊ณผ ๊ฒฐํ•ฉ์ด ๋˜์–ด์•ผ ํ•˜๋Š” ์ƒํ™ฉ์ด๋‹ˆ, ๋ฆฌํ„ฐ๋Ÿด 3์„ Double ํƒ€์ž…์œผ๋กœ ์ถ”๋ก ํ•œ๋‹ค๋Š” ๋ง ๊ฐ™์•„์š”.)

let pi = 3 + 0.141592

ํƒ€์ž… ๋ณ„์นญ (Type Aliases)

ํƒ€์ž… ๋ณ„์นญ์€ ์กด์žฌํ•˜๋Š” ํƒ€์ž…์˜ ์ด๋ฆ„์„ ๋‹ค๋ฅธ ๋ฐฉ์‹์œผ๋กœ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ณ , typealias ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

๊ธฐ์กด ํƒ€์ž…์„ ๋” ๋งž๋Š” ๋งฅ๋ฝ์—์„œ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ƒˆ๋กœ์šด ์ด๋ฆ„์œผ๋กœ ์ฐธ์กฐํ•  ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ํŠนํžˆ ์™ธ๋ถ€ ์†Œ์Šค์—์„œ ํŠน์ • ํฌ๊ธฐ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

typealias AudioSample = UInt16

์ƒˆ๋กญ๊ฒŒ ์ •์˜ํ•œ ํƒ€์ž…์€ ์›๋ž˜ ํƒ€์ž…์ด ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋Š” ์œ„์น˜์—์„œ ๋ชจ๋‘ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

AudioSample์€ UInt16์„ ๋Œ€์‹ ํ•˜์—ฌ ์ •์˜๋œ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

(โ—๏ธ๊ฐ„๋‹จํ•˜๊ฒŒ ์ƒ๊ฐํ•ด์„œ ์ˆ˜ํ•™์—์„œ ํ”ํžˆ ์‚ฌ์šฉํ•˜๋Š” ์น˜ํ™˜์„ ์ƒ๊ฐํ•˜๋ฉด ๋˜๊ณ , ์ฝ”๋“œ์— ๊ฐ€๋…์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.!)

var maxAmplitudeFound = AudioSample.min
// maxAmplitudeFound is now 0

์ถ”๊ฐ€์ ์ธ typealias ์‚ฌ์šฉ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

  1. Bytes ํƒ€์ž… ๋ณ„์นญ ์˜ˆ์‹œ:
    typealias Byte = UInt8
    
    ์ด๋ ‡๊ฒŒ ์ •์˜๋œ Byte ํƒ€์ž…์€ UInt8์˜ ๋ณ„์นญ์œผ๋กœ, ์ด์ œ ๋ฐ”์ดํŠธ ๋‹จ์œ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ Byte๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ๊ฐ€๋ น, ์™ธ๋ถ€ ๋ฐ์ดํ„ฐ ์†Œ์Šค์—์„œ ๋ฐ”์ดํŠธ(Byte) ๋‹จ์œ„๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฝ์–ด์˜ค๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ, ๋ฐ”์ดํŠธ ๋‹จ์œ„์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋” ์˜๋ฏธ์žˆ๋Š” ์ด๋ฆ„์œผ๋กœ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•ด ํƒ€์ž… ๋ณ„์นญ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ๋ฐฐ์—ด ํƒ€์ž… ๋ณ„์นญ ์˜ˆ์‹œ:
    typealias StringArray = [String]
    
    ์ด์ œ StringArray๋Š” [String]์˜ ๋ณ„์นญ์œผ๋กœ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ฝ”๋“œ์—์„œ ๋” ์˜๋ฏธ ์žˆ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

    ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ฒ˜๋ฆฌํ•  ๋•Œ ํƒ€์ž… ๋ณ„์นญ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์ž์—ด ๋ฐฐ์—ด์„ ๋” ๋ช…์‹œ์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

  3. ํŠœํ”Œ ํƒ€์ž… ๋ณ„์นญ ์˜ˆ์‹œ:
    typealias Coordinate = (x: Double, y: Double)
    
    ์ด์ œ Coordinate๋Š” (x: Double, y: Double)์˜ ๋ณ„์นญ์œผ๋กœ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

    ํŠœํ”Œ์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ๋ฐ์ดํ„ฐ ์œ ํ˜•์„ ๋ฌถ์–ด ํ‘œํ˜„ํ•  ๋•Œ๋„ ํƒ€์ž… ๋ณ„์นญ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์ขŒํ‘œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํŠœํ”Œ์„ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

ํƒ€์ž… ๋ณ„์นญ์€ ๊ธฐ์กด ํƒ€์ž…์— ์˜๋ฏธ ์žˆ๋Š” ์ด๋ฆ„์„ ๋ถ€์—ฌํ•˜์—ฌ ์ฝ”๋“œ๋ฅผ ์ฝ๊ธฐ ์‰ฝ๊ฒŒ ๋งŒ๋“ค๊ณ , ํŠนํžˆ ํŠน์ • ๋งฅ๋ฝ์—์„œ ๋” ์ ํ•ฉํ•œ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค๋‹ˆ๋‹ค.


๋ถ€์šธ (Booleans)

Swift๋Š” Bool์ด๋ผ ๋ถˆ๋ฆฌ๋Š” ๊ธฐ์ข€ Boolean type์„ ๊ฐ–์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ 2๊ฐœ์˜ Bool ๊ฐ’์ธ true, false๋ฅผ ์ œ๊ณตํ•˜๊ณ , ์ฐธ๊ณผ ๊ฑฐ์ง“์œผ๋กœ ํ‘œํ˜„๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋…ผ๋ฆฌ์ ์ธ ๊ฐ’์œผ๋กœ ์ฐธ์กฐ๋ฉ๋‹ˆ๋‹ค.

let orangesAreOrange = true
let turnipsAreDelicious = false

 

์œ„์—์„œ ์„ค๋ช…ํ•œ ๊ฒƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, Bool ํƒ€์ž…๋„ ํƒ€์ž… ์ถ”๋ก ์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— true๋‚˜ false ๊ฐ’์„ ๋ฏธ๋ฆฌ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค๋ฉด Bool ํƒ€์ž…์„ ์„ ์–ธํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

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

if turnipsAreDelicious {
    print("Mmm, tasty turnips!")
} else {
    print("Eww, turnips are horrible.")
}
// Prints "Eww, turnips are horrible."

 

Swift์˜ ํƒ€์ž… ์„ธ์ดํ”„ํ‹ฐ๋Š” ๋ถ€์šธ์ด ์•„๋‹Œ ๊ฐ’์ด Bool๋กœ ๋Œ€์ฒด๋˜๋Š” ๊ฒƒ์„ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ปดํŒŒ์ผ ์‹œ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.

let i = 1
if i {
    // this example will not compile, and will report an error
}

 

์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด์ฃผ๋ฉด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

i == 1 ๋น„๊ต ๊ฒฐ๊ณผ๋Š” Bool ํƒ€์ž…์ด๋ฏ€๋กœ, if๋ฌธ์˜ ์กฐ๊ฑด ์ ˆ์— ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

(โ—๏ธ๊ณ„์†ํ•ด์„œ ํƒ€์ž… ์„ธ์ดํ”„ํ‹ฐ๋ฅผ ๊ฐ•์กฐํ•˜๋Š” ๋Š๋‚Œ..๐Ÿ˜…)

let i = 1
if i == 1 {
    // this example will compile successfully
}

ํŠœํ”Œ (Tuples)

ํŠœํ”Œ์€ ์—ฌ๋Ÿฌ ๊ฐ’์„ ๋‹จ์ผ ๋ณตํ•ฉ ๊ฐ’์œผ๋กœ ๊ทธ๋ฃนํ™” ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ ํŠœํ”Œ ์•ˆ์— ๊ฐ’์€ ์–ด๋– ํ•œ ํƒ€์ž…๋„ ๊ฐ€๋Šฅํ•˜๋ฉฐ ์„œ๋กœ ๊ฐ™์€ ํƒ€์ž…์ผ ํ•„์š”๋Š” ์—†์Šต๋‹ˆ๋‹ค.

์•„๋ž˜ ์˜ˆ์‹œ๋Š” HTTP ์ƒํƒœ ์ฝ”๋“œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํŠœํ”Œ๋กœ, Int์™€ String ํƒ€์ž…์˜ ๊ฐ’์„ ํ•จ๊ป˜ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.

ํ•ด๋‹น ์ƒ์ˆ˜์˜ ํŠœํ”Œ ํƒ€์ž…์€ (Int, Stirng)์œผ๋กœ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let http404Error = (404, "Not Found")

 

๋ชจ๋“  ํƒ€์ž…์˜ ํŠœํ”Œ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์›ํ•˜๋Š” ๋งŒํผ ๋‹ค๋ฅธ ํƒ€์ž…์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

(โ—๏ธ์™œ ์—ฌ๊ธฐ์„œ permutation(: ์ˆœ์—ด) ๋‹จ์–ด๋ฅผ ์ผ๋Š”์ง€ ์˜๋ฌธ์ด์—ˆ๋Š”๋ฐ, ์ฐพ์•„๋ณด๋‹ˆ permutation์ด ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์œ ํ˜•์„ ์กฐํ•ฉํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋‹ค์–‘ํ•œ ์กฐํ•ฉ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค!)

 

(โ—๏ธ๋‹ค์‹œ ๋งํ•ด, ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ์œ ํ˜•์˜ ํƒ€์ž…๋“ค์„ ์กฐํ•ฉํ•˜์—ฌ ํŠœํ”Œ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค!)

 

ํŠœํ”Œ์˜ ๊ฐ’๋“ค์„ ๋ณ„๋„๋กœ ์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜๋กœ ๋ถ„ํ•ดํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

statusCode์— 404๊ฐ€, statusMessage์—๋Š” “Not Found”๊ฐ€ ์ €์žฅ๋œ ๊ฒ๋‹ˆ๋‹ค.

let (statusCode, statusMessage) = http404Error
print("The status code is \\(statusCode)")
// Prints "The status code is 404"
print("The status message is \\(statusMessage)")
// Prints "The status message is Not Found"

 

๋งŒ์•ฝ ํŠœํ”Œ์˜ ๊ฐ’๋“ค ์ค‘ ์ผ๋ถ€๋งŒ ํ•„์š”ํ•˜๋‹ค๋ฉด **๋ฐ‘์ค„(_)**์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

ํŠœํ”Œ์˜ ๋‘ ๋ฒˆ์งธ ๊ฐ’์ด ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— (_)์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„ํ•ดํ•  ๋•Œ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.

(โ—๏ธ์–ธ๋”๋ฐ”๋ฅผ ์“ฐ๋Š” ๊ฒƒ์„ wildcard pattern์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋Œ€์š”!)

// ๋ฐ‘์ค„(_)๋กœ ํŠœํ”Œ์˜ ์ผ๋ถ€ ๋ฌด์‹œ
let (justTheStatusCode, _) = http404Error
print("The status code is \\(justTheStatusCode)")
// Prints "The status code is 404"

 

๋˜๋Š”, 0์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํŠœํ”Œ์˜ ๊ฐœ๋ณ„ ์š”์†Œ ๊ฐ’์— ์ ‘๊ทผ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

print("The status code is \\(http404Error.0)")
// Prints "The status code is 404"
print("The status message is \\(http404Error.1)")
// Prints "The status message is Not Found"

 

ํŠœํ”Œ์„ ์ •์˜ํ•  ๋•Œ, ๊ฐ๊ฐ์˜ ์š”์†Œ์— ์ด๋ฆ„์„ ์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

let http200Status = (statusCode: 200, description: "OK")

 

๋งŒ์•ฝ ํŠœํ”Œ ์š”์†Œ์— ์ด๋ฆ„์ด ์žˆ๋‹ค๋ฉด, ์š”์†Œ์˜ ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

print("The status code is \\(http200Status.statusCode)")
// Prints "The status code is 200"
print("The status message is \\(http200Status.description)")
// Prints "The status message is OK"

 

ํŠœํ”Œ์€ ํ•จ์ˆ˜์˜ ๋ฐ˜ํ™˜ ๊ฐ’์œผ๋กœ ํŠนํžˆ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค. ์›น ํŽ˜์ด์ง€๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ํ•จ์ˆ˜๋Š” ์„ฑ๊ณต์ด๋‚˜ ์‹คํŒจ๋ฅผ ์ „๋‹ฌํ•˜๊ธฐ ์œ„ํ•ด (Int, String) ํŠœํ”Œ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ์ด ํ•จ์ˆ˜๋Š” ๊ฐ๊ฐ ๋‹ค๋ฅธ ํƒ€์ž…์˜ 2๊ฐ€์ง€ ๊ณ ์œ  ๊ฐ’์„ ๊ฐ™๋Š” ํŠœํ”Œ์„ ๋ฆฌํ„ดํ•˜์—ฌ ๋‹จ์ผ ๊ฐ’๋งŒ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜๋ณด๋‹ค ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

ํŠœํ”Œ์€ ๋ณต์žกํ•œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ์—๋Š” ์ ํ•ฉํ•˜์ง€ ์•Š๊ณ , ๊ด€๋ จ๋œ ๊ฐ’์˜ ๊ฐ„๋‹จํ•œ ๊ทธ๋ฃน์„ ๋งŒ๋“ค ๋•Œ ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

๋งŒ์•ฝ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๊ฐ€ ๋ณต์žกํ•œ ๊ฒฝ์šฐ์—๋Š” ํŠœํ”Œ์ด ์•„๋‹Œ ํด๋ž˜์Šค๋‚˜ ๊ตฌ์กฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.


์˜ต์…”๋„ (Optionals)

๊ฐ’์ด ์—†๋Š” ๊ฒฝ์šฐ์— ์˜ต์…”๋„์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์˜ต์…”๋„์€ ๋‘ ๊ฐ€์ง€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋Š”๋ฐ, ํ•˜๋‚˜๋Š” ์ง€์ •๋œ ํƒ€์ž…์˜ ๊ฐ’์ด ์žˆ์–ด ์˜ต์…”๋„์„ ์–ธ๋ž˜ํ•‘ํ•ด์„œ ๊ฐ’์— ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด๊ณ , ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ๊ฐ’์ด ์—†๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค.

 

์•„๋ž˜ ์˜ˆ์‹œ๋Š” String์„ Int๋กœ ์ดˆ๊ธฐํ™”๋Š” ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ String ํƒ€์ž…์˜ ๊ฐ’์ด Int๋กœ ์ดˆ๊ธฐํ™” ๋  ๋•Œ ์‹คํŒจํ•  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์— Int ํƒ€์ž…์ด ์•„๋‹Œ ์˜ต์…”๋„ Intํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๊ฒ๋‹ˆ๋‹ค.

(โ—๏ธ๋งŒ์•ฝ Stirng ํƒ€์ž…์˜ ๊ฐ’์ด “Hello”๋ผ๋ฉด ์ดˆ๊ธฐํ™”๊ฐ€ ์‹คํŒจ๋  ๊ฒƒ์ž…๋‹ˆ๋‹ค.)

let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
// The type of convertedNumber is "optional Int"

nil

์˜ต์…”๋„ ๋ณ€์ˆ˜์— ๊ฐ’์ด ์—†์Œ์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด nil๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

var serverResponseCode: Int? = 404
// serverResponseCode contains an actual Int value of 404
serverResponseCode = nil
// serverResponseCode now contains no value

 

๊ธฐ๋ณธ๊ฐ’ ์—†์ด ์˜ต์…”๋„ ํƒ€์ž…์˜ ๋ณ€์ˆ˜๊ฐ€ ์ง€์ •์ด ๋œ๋‹ค๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ nil ๊ฐ’์ด ์„ ์–ธ๋ฉ๋‹ˆ๋‹ค.

var surveyAnswer: String?
// surveyAnswer is automatically set to nil

 

if ๋ฌธ์œผ๋กœ ๊ฐ™์Œ ์—ฐ์‚ฐ์ž (==) ๋˜๋Š” ๊ฐ™์ง€ ์•Š์€ ์—ฐ์‚ฐ์ž (! =)์„ ์‚ฌ์šฉํ•˜์—ฌ ์˜ต์…”๋„์ด ๊ฐ’์„ ๊ฐ–๋Š”์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

nil๊ณผ ๊ฐ™์ง€ ์•Š๋‹ค๋ฉด ์˜ต์…”๋„์€ ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๋Š” ๊ฒ๋‹ˆ๋‹ค.

let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)

if convertedNumber != nil {
    print("convertedNumber contains some integer value.")
}
// Prints "convertedNumber contains some integer value."

 

์˜ต์…”๋„ ํƒ€์ž…์ด ์•„๋‹Œ ๋ณ€์ˆ˜๋‚˜ ์ƒ์ˆ˜์—๋Š” nil์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์˜ต์…”๋„์ด ์•„๋‹Œ ๊ฐ’์— nil์„ ํ• ๋‹นํ•œ๋‹ค๋ฉด ์ปดํŒŒ์ผ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์˜ต์…”๋„๊ณผ ์˜ต์…”๋„์ด ์•„๋‹Œ ๊ฐ’์˜ ๋ถ€๋ฆฌ๋Š” ๋ˆ„๋ฝ๋œ ์ •๋ณด๋ฅผ ๋ˆ„๋ฝ๋œ ์ •๋ณด๋ฅผ ๋ถ„๋ช…ํžˆ ํ‘œ์‹œํ•  ์ˆ˜ ์žˆ๊ณ , ์ด๋Ÿฐ ์‹ค์ˆ˜๋Š” ์ปดํŒŒ์ผ ์‹œ๊ฐ„์— ์˜ค๋ฅ˜๋ฅผ ๋ฐœ์ƒํ•˜๋ฏ€๋กœ ์˜ต์…”๋„์„ ์˜ต์…”๋„์ด ์•„๋‹Œ ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

 

์˜ต์…”๋„ˆ ๊ฐ’์— ์ ‘๊ทผํ•  ๋•Œ, nil๊ณผ nil์ด ์•„๋‹Œ ๊ฒƒ์— ๋Œ€ํ•ด ๋ชจ๋‘ ๋‹ค๋ค„์•ผ ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ’์ด ์—†์„ ๋•Œ ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ์‹์ด ๋ช‡ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  • nil์ผ ๋•Œ ํ•ด๋‹น ๊ฐ’์— ๋Œ€ํ•œ ๋™์ž‘์„ ์Šคํ‚ตํ•ฉ๋‹ˆ๋‹ค.
  • nil์„ ๋ฆฌํ„ดํ•˜๊ฑฐ๋‚˜, ์˜ต์…”๋„ ์ฒด์ธ์—์„œ ์„ค๋ช…ํ•  ? ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ nil์„ ์ „ํŒŒํ•ฉ๋‹ˆ๋‹ค.
  • ?? ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋Œ€์ฒด ๊ฐ’(fallback value)์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
  • ! ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ ์‹คํ–‰์„ ๋ฉˆ์ถฅ๋‹ˆ๋‹ค.

Objective-C์—์„œ nil์€ ๊ฐ์ฒด๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š๋Š” ํฌ์ธํ„ฐ์ด์ง€๋งŒ, Swift์—์„œ๋Š” ํฌ์ธํ„ฐ๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค.

ํŠน์ • ํƒ€์ž…์˜ ๊ฐ’์ด ์—†์Œ์„ ๋‚˜ํƒ€๋‚ด๊ณ , ๋ชจ๋“  ์˜ต์…”๋„ ํƒ€์ž…์€ ๊ฐ์ฒด ํƒ€์ž… ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ๋ชจ๋‘ nil๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ (Optional Binding)

์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์€ ์˜ต์…”๋„์ด ๊ฐ’์„ ํฌํ•จํ•˜๊ณ  ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๊ณ , ๊ฐ’์ด ์žˆ๋‹ค๋ฉด ์ผ์‹œ์ ์œผ๋กœ ์ƒ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ๋ฐฉ์‹์ž…๋‹ˆ๋‹ค.

if, guard, while๋ฌธ์„ ํ†ตํ•ด ๋‚ด๋ถ€์— ๊ฐ’์ด ์žˆ๋Š”์ง€ ์ฒดํฌํ•˜๊ณ  ๋‹จ์ผ ๋™์ž‘์˜ ์ผ๋ถ€๋กœ ์ƒ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋กœ ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์•„๋ž˜ ์˜ˆ์‹œ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด ์˜ต์…”๋„ ํƒ€์ž…์˜ someOptional์ด ๊ฐ’์„ ๊ฐ–๊ณ  ์žˆ๋‹ค๋ฉด contantName์œผ๋กœ ๋ฐ”์ธ๋”ฉ์ด ๋˜์–ด

if ๊ตฌ๋ฌธ ๋‚ด์—์„œ ๊ทธ ๊ฐ’์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

if let <#constantName#> = <#someOptional#> {
   <#statements#>
}

 

๊ฐ’์— ์ ‘๊ทผํ•œ ํ›„์— ๊ธฐ์กด ์˜ต์…”๋„ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์ฐธ์กฐํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค๋ฉด ๊ฐ™์€ ์ด๋ฆ„์œผ๋กœ ์ƒˆ๋กœ์šด ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

if let myNumber = myNumber {
    // Here, myNumber is a non-optional integer
    print("My number is \\(myNumber)")
}

 

์œ„์˜ ์ฝ”๋“œ๋ฅผ ๋” ์งง๊ฒŒ ์‚ฌ์šฉํ•˜์—ฌ ์˜ต์…”๋„ ๊ฐ’์„ ์–ธ๋ž˜ํ•‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

if let myNumber {
    print("My number is \\(myNumber)")
}
// Prints "My number is 123"

 

์œ„์— ์˜ˆ์ œ์ฝ”๋“œ์—์„œ ๊ณ„์† let์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ”์ธ๋”ฉํ•˜๊ณ  ์žˆ๋Š”๋ฐ, var๋ฅผ ์“ฐ๋ฉด ๋ณ€์ˆ˜๋กœ ๋ฐ”์ธ๋”ฉํ•ด์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งŒ์•ฝ ํ•ด๋‹น ๋ณ€์ˆ˜๋ฅผ if๋ฌธ ๋‚ด์—์„œ ๊ฐ’์„ ๋ฐ”๊พผ๋‹ค๋ฉด ์ด๋Š” ์ง€์—ญ ๋ณ€์ˆ˜์—๋งŒ ์ ์šฉ์ด๋˜๊ณ , ๊ธฐ์กด ์–ธํŒจ๋ง๋œ ์˜ต์…”๋„ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜์— ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

let myName: String? = "Leo"

if var myName {
    myName = "ํ™๊ธธ๋™"
    print("์ง€์—ญ ๋ณ€์ˆ˜: \\(myName)")
}

// ๊ฐ•์ œ ์–ธ๋ž˜ํ•‘
print(myName!)

 

if ๊ตฌ๋ฌธ์— ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ๊ณผ ์กฐ๊ฑด ํŒ๋‹จ์„ ์—ฌ๋Ÿฌ ๊ฐœ ํฌํ•จํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ ๊ฐ’ ์ค‘ nil์ด ์žˆ๊ฑฐ๋‚˜, ์กฐ๊ฑด ๋ฌธ์— false๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์žˆ๋‹ค๋ฉด ์ „์ฒด if๋ฌธ์˜ ์กฐ๊ฑด์€ fasle๋กœ ๊ฐ„์ฃผ๋ฉ๋‹ˆ๋‹ค.

if let firstNumber = Int("4"), let secondNumber = Int("42"), firstNumber < secondNumber && secondNumber < 100 {
    print("\\(firstNumber) < \\(secondNumber) < 100")
}
// Prints "4 < 42 < 100"

if let firstNumber = Int("4") {
    if let secondNumber = Int("42") {
        if firstNumber < secondNumber && secondNumber < 100 {
            print("\\(firstNumber) < \\(secondNumber) < 100")
        }
    }
}
// Prints "4 < 42 < 100"

 

if๋ฌธ์œผ๋กœ ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉํ•œ ์ƒ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋Š” ํ•ด๋‹น ๊ตฌ๋ฌธ์˜ ๋ณธ๋ฌธ์—์„œ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ guard ๊ตฌ๋ฌธ์œผ๋กœ ์˜ต์…”๋„ ๋ฐ”์ธ๋”ฉ์„ ํ•œ๋‹ค๋ฉด guard ๊ตฌ๋ฌธ ๋ฐ–์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

let myName: String? = "Leo"
let myAge: Int? = nil

func useGuardOptionalBinding() {
    guard let myName else {
        print("nil์ž…๋‹ˆ๋‹ค.")
        return
    }
    
    // ๋ฐ”์ธ๋”ฉ๋œ myName์„ guard๋ฌธ ๋ฐ–์—์„œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    print(myName)
    
    guard let myAge else {
        // ๊ฐ’์ด ์—†์œผ๋ฏ€๋กœ guard๋ฌธ์˜ else ๊ตฌ๋ฌธ์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค.
        print("nil์ž…๋‹ˆ๋‹ค.")
     return
    }

    // ์œ„์— guard๋ฌธ์—์„œ ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ์ข…๋ฃŒ๋˜์–ด ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
    print(myAge)
    
}

useGuardOptionalBinding()

๋Œ€์ฒด๊ฐ’ ์ œ๊ณต (provide a Fallback Value)

์˜ต์…”๋„ ํƒ€์ž…์ด ๊ฐ’์ด ์—†๋Š” ์ƒํ™ฉ์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด nil-๊ฒฐํ•ฉ ์—ฐ์‚ฐ์ž (??)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋””ํดํŠธ ๊ฐ’์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์˜ต์…”๋„์—์„œ ?? ์˜ ์™ผํŽธ์ด nil ์ด ์•„๋‹ˆ๋ฉด, ๊ฐ’์€ ์–ธ๋ž˜ํ•‘๋˜๊ณ  ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ?? ์˜ ์˜ค๋ฅธํŽธ์— ๊ฐ’์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

let name: String? = nil
let greeting = "Hello, " + (name ?? "friend") + "!"
print(greeting)
// Prints "Hello, friend!"

๊ฐ•์ œ ์–ธ๋ž˜ํ•‘ (Force Unwrapping)

nil์ด ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ์—๋Ÿฌ๋‚˜ ์›์น˜์•Š์€ ์ƒํƒœ์™€ ๊ฐ™์ด ํšŒ๋ณตํ•  ์ˆ˜ ์—†๋Š” ์‹คํŒจ๋ฅผ ๋‚˜ํƒ€๋‚ผ ๋•Œ, ์˜ต์…”๋„์˜ ๋’ค์— ๋Š๋‚Œํ‘œ (!)๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๊ฒƒ์„ ๊ฐ•์ œ ์–ธ๋ž˜ํ•‘์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ nil์„ ๊ฐ•์ œ ์–ธ๋ž˜ํ•‘ํ•œ๋‹ค๋ฉด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๋ฅผ ์œ ๋ฐœํ•ฉ๋‹ˆ๋‹ค.

 

(โ—๏ธ์‚ฌ์‹ค ์ฒ˜์Œ์—๋Š” ์ด ๋ง์ด ์ดํ•ด๋˜์ง€ ์•Š์•˜์Šต๋‹ˆ๋‹ค๋งŒ, ๊ณ„์† ๊ณ ๋ฏผํ•ด๋ณธ ๊ฒฐ๊ณผ

์ด ๋ง์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ์˜ ์‹ค์ˆ˜๋‚˜ ํšŒ๋ณตํ•  ์ˆ˜ ์—†๋Š” ์‹คํŒจ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค๋Š” ๊ฒƒ์ธ๋ฐ, ์ด๋Š” ํ•ด๋‹น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ๋” ์ด์ƒ ์•ฑ์„ ์ง€์†ํ•˜๋Š” ๊ฒƒ์ด ๋ถ€์ ์ ˆํ•˜๊ณ  ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐ•์ œ ์ข…๋ฃŒ์‹œ์ผœ์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ด ์ƒํ™ฉ์—์„œ๋Š” ๊ฐ•์ œ ์–ธ๋ž˜ํ•‘์„ ์‚ฌ์šฉํ•˜์—ฌ nil์„ ํ™•์ธํ•˜๊ณ , nil์„ ํ†ตํ•ด ์‹ฌ๊ฐํ•œ ๋ฌธ์ œ๊ฐ€ ํ™•์ธ๋˜๋ฉด ์•ฑ์„ ์ข…๋ฃŒ์‹œ์ผœ๋ฒ„๋ฆฌ๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.)

 

(โ—๏ธํ•˜์ง€๋งŒ, ๊ฐ•์ œ ์ข…๋ฃŒ๋Š” ์‚ฌ์šฉ์ž์—๊ฒŒ ์ข‹์€ ๊ฒฝํ—˜์„ ์ œ๊ณตํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ๊ฐœ๋ฐœ์ž๋Š” ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์— ๋Œ€๋น„ํ•˜์—ฌ ์•ฑ์„ ๊ฐ•์ œ๋กœ ์ข…๋ฃŒ์‹œํ‚ค๋Š” ๋Œ€์‹  ๋˜ ๋‹ค๋ฅธ ํ•ด๊ฒฐ์ฑ…์„ ๋งˆ๋ จํ•˜๋Š” ๊ฒƒ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค!!)

 

(โ—๏ธ๋ฐœ์ƒ ํ• ๋งŒํ•œ ๋ฌธ์ œ๋ฅผ ๋ฏธ๋ฆฌ ์ƒ๊ฐํ•˜๊ณ , ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด๋‘ฌ์•ผ๊ฒ ์ฃ ??)

 

๋Š๋‚Œํ‘œ(!)๋Š” fatalError(_:file:line:)์˜ ์งง์€ ์ž‘์„ฑ๋ฒ•์ž…๋‹ˆ๋‹ค.

fatalError(_:file:line:) | Apple Developer Documentation

let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)

let number = convertedNumber!

// converedNumber๊ฐ€ Nil์ผ ๊ฒฝ์šฐ else ๊ตฌ๋ฌธ ์‹คํ–‰
guard let number = convertedNumber else {
    fatalError("The number was invalid")
}

์•”์‹œ์ ์œผ๋กœ ์–ธ๋ž˜ํ•‘๋œ ์˜ต์…”๋„ (Implicitly Unwrapped Optionals)

ํ”„๋กœ๊ทธ๋žจ ๊ตฌ์กฐ์ƒ ์˜ต์…”๋„์ด ๊ฐ’์„ ์ฒ˜์Œ ์„ค์ •ํ•œ ํ›„์— ํ•ญ์ƒ ๊ฐ’์„ ๊ฐ–๋Š” ๊ฒƒ์ด ๋ถ„๋ช…ํ•  ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ ํ•ญ์ƒ ๊ฐ’์ด ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ ‘๊ทผํ•  ๋•Œ๋งˆ๋‹ค ์˜ต์…”๋„์˜ ๊ฐ’์„ ํ™•์ธํ•˜๊ณ  ์–ธ๋ž˜ํ•‘ํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

 

์ด๋Ÿฌํ•œ ์˜ต์…”๋„์„ ์•”์‹œ์ ์œผ๋กœ ์–ธ๋ž˜ํ•‘ ๋œ ์˜ต์…” (implicitly unwrapped optionals)๋กœ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

์˜ต์…”๋„ ํƒ€์ž…์„ ๋งŒ๋“ค ๋•Œ ๋ฌผ์Œํ‘œ(?) ๋Œ€์‹  ๋Š๋‚Œํ‘œ(!)๋ฅผ ์จ์„œ ์•”์‹œ์ ์œผ๋กœ ์–ธ๋ž˜ํ•‘ ๋œ ์˜ต์…”๋„์„ ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

ํ•˜์ง€๋งŒ, ์•”์‹œ์ ์œผ๋กœ ์–ธ๋ž˜ํ•‘๋œ ์˜ต์…”๋„์ด nil์ผ ๊ฒฝ์šฐ์— ์ ‘๊ทผํ•œ๋‹ค๋ฉด ๋Ÿฐํƒ€์ž„ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์‚ฌ์šฉํ•  ๋•Œ ์˜ต์…”๋„ ์ด๋ฆ„์˜ ๋’ค์— ๋Š๋‚Œํ‘œ๋ฅผ ์œ„์น˜์‹œํ‚ค๋Š” ๊ฒƒ๋ณด๋‹ค ์„ ์–ธํ•  ๋•Œ ์˜ต์…”๋„ ํƒ€์ž… ๋’ค์— ๋Š๋‚Œํ‘œ๋ฅผ ์œ„์น˜์‹œํ‚ค๋Š” ๊ฒƒ์ด ๋” ์ข‹์Šต๋‹ˆ๋‹ค.

 

(โ—๏ธ๊ฐ•์ œ ์–ธ๋ž˜ํ•‘์„ ํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค, ์ฒ˜์Œ๋ถ€ํ„ฐ ์•”์‹œ์ ์œผ๋กœ ์–ธ๋ž˜ํ•‘๋œ ์˜ต์…”๋„ ํƒ€์ž…์œผ๋กœ ์„ ์–ธํ•˜๋Š” ๊ฒŒ ๋” ์ข‹๋‹ค๋Š” ๋ง์ž…๋‹ˆ๋‹ค.)

 

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

 

์•”์‹œ์ ์œผ๋กœ ์–ธ๋ž˜ํ•‘๋œ ์˜ต์…”๋„ ์‚ฌ์šฉ์ด ์˜ต์…”๋„์„ ๊ฐ•์ œ๋กœ ์–ธ๋ž˜ํ•‘์„ ํ—ˆ๋ฝํ•˜๋Š” ๊ฒƒ์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์•„๋‹™๋‹ˆ๋‹ค.

 

Swift๋Š” ํ•ด๋‹น ๊ฐ’์„ ์‚ฌ์šฉํ•  ๋•Œ ๊ธฐ์กด์˜ ์˜ต์…”๋„ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•˜๊ณ  ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€๋Šฅํ•  ๊ฒฝ์šฐ Swift๋Š” ๊ฐ•์ œ ์–ธ๋ž˜ํ•‘์„ ํ•ฉ๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์•„๋ž˜์˜ implicitString์€ String ํƒ€์ž…, optionalString์€ String? ํƒ€์ž…์œผ๋กœ ์„œ๋กœ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.

let possibleString: String? = "An optional string."
let forcedString: String = possibleString! // Requires explicit unwrapping

let assumedString: String! = "An implicitly unwrapped optional string."
let implicitString: String = assumedString // Unwrapped automatically
let optionalString = assumedString
// The type of optionalString is "String?" and assumedString isn't force-unwrapped.

 

(โ—๏ธ์š”๊ฑด ์ฒ˜์Œ ์•Œ์•˜์–ด์š”.. ๋ฌด์Šจ ๋ง์ด๋ƒ๋ฉด, ์•”์‹œ์ ์œผ๋กœ ์–ธ๋ž˜ํ•‘๋œ ์˜ต์…”๋„ ํƒ€์ž…์˜ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ•  ๋•Œ ํ•ด๋‹น ๊ฐ’์„ ์˜ต์…”๋„ ํƒ€์ž…์˜ ๊ฐ’์œผ๋กœ ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค๋ฉด Swift๋Š” ์˜ต์…”๋„ ๊ฐ’์„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์˜ต์…”๋„ ๊ฐ’์„ ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ์—๋Š” Swift๊ฐ€ ์–ธ๋ž˜ํ•‘์„ ํ•ด์„œ ๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค๋Š” ๋ง์ž…๋‹ˆ๋‹ค.)

(โ—๏ธ๋” ๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•ด์„œ ์•”์‹œ์ ์œผ๋กœ ์–ธ๋ž˜ํ•‘๋œ ์˜ต์…”๋„ ํƒ€์ž…์˜ ๊ฐ’์„ ์‚ฌ์šฉํ•  ๋•Œ, ๊ฐ’์ด ์˜ต์…”๋„ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•ด๋„ ๋˜๋Š” ๊ฒฝ์šฐ ๊ทธ๋ ‡๊ฒŒ ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š๋‹ค๋ฉด ๊ฐ•์ œ ์–ธ๋ž˜ํ•‘์„ ํ†ตํ•ด ํ•ด๋‹น ๊ฐ’์„ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ํ›„์—๋Š” ํ•ด๋‹น ๊ฐ’์„ ์‚ฌ์šฉํ•  ๋•Œ ์–ธ๋ž˜ํ•‘๋œ ๊ฐ’์œผ๋กœ ์ทจ๊ธ‰๋ฉ๋‹ˆ๋‹ค!)

(โ—๏ธ์•„๋ž˜ print๋ฌธ์—์„œ assumedString์˜ ๊ฐ’์„ ์–ธ๋ž˜ํ•‘ํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์˜ต์…”๋„ ๊ฐ’ ๊ทธ๋Œ€๋กœ ํ”„๋ฆฐํŠธ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.)

let assumedString: String! = "An implicitly unwrapped optional string."

print(assumedString)
// ์ถœ๋ ฅ: Optional("An implicitly unwrapped optional string.")

์—๋Ÿฌ ์ฒ˜๋ฆฌ (Error Handling)

ํ”„๋กœ๊ทธ๋žจ์ด ์‹คํ–‰๋˜๋Š” ๋™์•ˆ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ๋•Œ๋ฅผ ๋Œ€์‘ํ•˜๊ธฐ ์œ„ํ•ด ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

๊ฐ’์˜ ์กด์žฌ ์œ ๋ฌด๋กœ ํ•จ์ˆ˜์˜ ์„ฑ๊ณต ์—ฌ๋ถ€๋ฅผ ์•Œ๋ ค์ฃผ๋Š” ์˜ต์…”๋„๊ณผ๋Š” ๋‹ฌ๋ฆฌ, ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋Š” ์‹คํŒจ์˜ ์›์ธ์„ ์•Œ ์ˆ˜ ์žˆ๊ณ  ๋‹ค๋ฅธ ๊ณณ์œผ๋กœ ์—๋Ÿฌ๋ฅผ ์ „ํŒŒํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

 

ํ•จ์ˆ˜๊ฐ€ ์—๋Ÿฌ ์กฐ๊ฑด์„ ๋งŒ๋‚˜๋ฉด ์—๋Ÿฌ(error)๋ฅผ ๋˜์ง€๊ณ , ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ์ž๋Š” ์—๋Ÿฌ๋ฅผ ํฌ์ฐฉ(catch)ํ•˜๊ณ  ์ ์ ˆํžˆ ๋Œ€์‘ํ•ฉ๋‹ˆ๋‹ค.

ํ•จ์ˆ˜์˜ ์„ ์–ธ์— throws ํ‚ค์›Œ๋“œ๋ฅผ ํฌํ•จ์‹œ์ผœ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ด๊ณ , ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ๋Š” ํ‘œํ˜„์‹ ์•ž์— try ํ‚ค์›Œ๋“œ๋ฅผ ๋ถ™์ž…๋‹ˆ๋‹ค(= ๋ž˜ํ•‘ํ•ฉ๋‹ˆ๋‹ค).

 

๋˜ํ•œ, do ๊ตฌ๋ฌธ์€ ์—๋Ÿฌ๋ฅผ ํ•˜๋‚˜ ์ด์ƒ์˜ catch ์ ˆ๋กœ ์ „ํŒŒํ•  ์ˆ˜ ์žˆ๋Š” ์ƒˆ๋กœ์šด ๋ฒˆ์œ„๋ฅผ ๋งŒ๋“ค๊ณ , Swift๋Š” ๋˜์ ธ์ง„ ์—๋Ÿฌ๊ฐ€ catch ์ ˆ์— ์˜ํ•ด ์ฒ˜๋ฆฌ๋  ๋•Œ๊นŒ์ง€ ์—๋Ÿฌ๋ฅผ ์ž๋™์œผ๋กœ ์ „ํŒŒํ•ฉ๋‹ˆ๋‹ค.

func makeASandwich() throws {
    // ...
}

do {
    try makeASandwich()
    eatASandwich()
} catch SandwichError.outOfCleanDishes {
    washDishes()
} catch SandwichError.missingIngredients(let ingredients) {
    buyGroceries(ingredients)
}

์—ญ์„ค๊ณผ ์ „์ œ ์กฐ๊ฑด (Assertions and Preconditions)

Assertion๊ณผ Precondition์€ ๋Ÿฐํƒ€์ž„์— ๋ฐœ์ƒํ•˜๋Š” ์‚ฌํ•ญ์„ ํ™•์ธํ•˜๋Š” ๋„๊ตฌ์ž…๋‹ˆ๋‹ค. ์ถ”๊ฐ€ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์ „์— ํ•„์ˆ˜ ์กฐ๊ฑด์ด ์ถฉ์กฑ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋งŒ์•ฝ Assertion ๋˜๋Š” Precondition์—์„œ false๋กœ ํŒ๋‹จ๋œ๋‹ค๋ฉด ํ”„๋กœ๊ทธ๋žจ์€ ์ข…๋ฃŒ๋ฉ๋‹ˆ๋‹ค. Assertion์€ ๊ฐœ๋ฐœ ๊ณผ์ •์—์„œ ์‹ค์ˆ˜๋‚˜ ์ž˜๋ชป๋œ ๊ฐ€์ •์„ ์ฐพ๋Š” ๋ฐ ๋„์›€์ด ๋˜๋ฉฐ, Precondition์€ ํ”„๋กœ๋•์…˜์—์„œ ์ด์Šˆ๋ฅผ ๊ฐ์ง€ํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.

 

์—๋Ÿฌ ์ฒ˜๋ฆฌ์™€๋Š” ๋‹ฌ๋ฆฌ Assertion๊ณผ Precondition์€ ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋˜์ง€ ์•Š๊ณ , ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ํ”„๋กœ๊ทธ๋žจ์„ ์ฆ‰์‹œ ์ข…๋ฃŒ์‹œํ‚ต๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐ€์ •์„ ํ™•์ธํ•˜์ง€ ์•Š๊ณ  ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•˜์—ฌ ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ๊ฐ€ ์†์ƒ๋˜๊ธฐ ์ „์— ๋ฌธ์ œ๋ฅผ ๋น ๋ฅด๊ฒŒ ๊ฐ์ง€ํ•˜์—ฌ ์˜ˆ๋ฐฉํ•˜๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค.

 

Assertion๊ณผ Precondition์˜ ์ฃผ์š” ์ฐจ์ด๋Š” ์–ธ์ œ ์ฒดํฌ๋˜๋Š”์ง€์— ์žˆ์Šต๋‹ˆ๋‹ค. Assertion์€ ์ฃผ๋กœ ๋””๋ฒ„๊ทธ ๋นŒ๋“œ์—์„œ๋งŒ ํ™•์ธ๋˜์ง€๋งŒ, Precondition์€ ๋””๋ฒ„๊ทธ์™€ ํ”„๋กœ๋•์…˜ ๋นŒ๋“œ์—์„œ ๋ชจ๋‘ ๊ฒ€์‚ฌ๋ฉ๋‹ˆ๋‹ค.

 

Assertion์€ ํ”„๋กœ๋•์…˜ ๋นŒ๋“œ์—์„œ ํ™•์ธ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ์„ฑ๋Šฅ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š๊ณ  ๊ฐœ๋ฐœ ๋‹จ๊ณ„์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


์—ญ์„ค์„ ํ†ตํ•œ ๋””๋ฒ„๊น… (Debugging with Assertions)

Swift ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ assert(_:_:file:line:) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

์ด ํ•จ์ˆ˜์— true ๋˜๋Š” false๋กœ ํŒ๋‹จ๋  ํ‘œํ˜„์‹๊ณผ false์ผ ๊ฒฝ์šฐ ์ถœ๋ ฅํ•  ๋ฉ”์„ธ์ง€๋ฅผ ์ „๋‹ฌํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

let age = -3
assert(age >= 0, "A person's age can't be less than zero.")
// This assertion fails because -3 isn't >= 0.

assert(::file:line:) | Apple Developer Documentation

 

๋˜ํ•œ, ๋ฉ”์‹œ์ง€๋ฅผ ์ƒ๋žตํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

assert(age >= 0)

 

์ฝ”๋“œ๊ฐ€ ์ด๋ฏธ assertion ์กฐ๊ฑด์„ ์ฒดํฌํ–ˆ๋‹ค๋ฉด assertionFailure(_:file:line:) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

if age > 10 {
    print("You can ride the roller-coaster or the ferris wheel.")
} else if age >= 0 {
    print("You can ride the ferris wheel.")
} else {
    assertionFailure("A person's age can't be less than zero.")
}

assertionFailure(_:file:line:) | Apple Developer Documentation


๊ฐ•์ œ ์ „์ œ์กฐ๊ฑด (Enforcing Preconditions)

precondition์€ ์ฝ”๋“œ๊ฐ€ ์‹คํ–‰๋˜๊ธฐ ์œ„ํ•ด์„œ ๋ฐ˜๋“œ์‹œ ์ฐธ์ธ ๊ฒฝ์šฐ์—๋งŒ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์šฉํ•˜๋Š” ๊ฐ’๋“ค์ด ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜๊ฑฐ๋‚˜ ์œ ํšจ ๊ฐ’์ด ์ „๋‹ฌ๋˜๋Š”์ง€๋ฅผ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

precondition(::file:line:) ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹คํŒจ๋ฅผ ์•Œ๋ฆด ์ˆ˜ ์žˆ๊ณ , ๋งŒ์•ฝ ์กฐ๊ฑด์ด false๋ผ๋ฉด ์„ค์ •ํ•ด๋‘” ์˜ค๋ฅ˜ ๊ตฌ๋ฌธ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

// In the implementation of a subscript...
precondition(index > 0, "Index must be greater than zero.")

 

(-Ounchecked) ๋ชจ๋“œ๋ฅผ ์ฒดํฌํ•˜์ง€ ์•Š๊ณ  ์ปดํŒŒ์ผ์„ ํ•œ๋‹ค๋ฉด, precondition์€ ์ฒดํฌ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์ปดํŒŒ์ผ๋Ÿฌ๋Š” ํ•ญ์ƒ precondition์„ ํ•ญ์ƒ true๋ผ๊ณ  ๊ฐ€์ •ํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์ตœ์ ํ™”ํ•ฉ๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿฌ๋‚˜ fatalError(_:file:line:) ํ•จ์ˆ˜๋Š” ์ตœ์ ํ™” ์„ค์ •๊ณผ ๋ฌด๊ด€ํ•˜๊ฒŒ ํ•ญ์ƒ ์‹คํ–‰์„ ์ค‘์ง€์‹œํ‚ต๋‹ˆ๋‹ค.

 

ํ”„๋กœํ† ํƒ€์ž…๊ณผ ์ดˆ๊ธฐ ๊ฐœ๋ฐœ๋‹จ๊ณ„์—์„œ ์•„์ง ๊ตฌํ˜„๋˜์ง€ ์•Š์€ ๊ธฐ๋Šฅ์—์„œ fatalError(_:file:line:) ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ fatalError("Unimplemented") ์™€ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

assertions๊ณผ preconditions์™€ ๋‹ฌ๋ฆฌ, fatal error๋Š” ์ ˆ๋Œ€ ์ตœ์ ํ™” ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ๋งŒ๋‚˜๋ฉด ํ•ญ์ƒ ํ”„๋กœ๊ทธ๋žจ์ด ์ค‘์ง€๋ฉ๋‹ˆ๋‹ค.

 


๋งˆ๋ฌด๋ฆฌ

์ด๊ฑฐ ์–ธ์ œ ๋‹ค ๋ณด๊ณ , ์–ธ์ œ ๋‹ค ์ •๋ฆฌํ•˜์ฃ ..?

์ƒ๊ฐ๋ณด๋‹ค ์—„์ฒญ ๊ธธ์–ด์š”.. ๊ทธ๋ž˜๋„ ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ๋„ ์žˆ๊ณ  ๋จธ๋ฆฌ์— ์ •๋ฆฌ๋„ ์ž˜ ๋˜๋Š” ๋Š๋‚Œ์ด๋ผ..

๊พธ์ค€ํ•˜๊ฒŒ ์ž‘์„ฑํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜…


์ž˜๋ชป๋œ ๋‚ด์šฉ์ด๋‚˜ ๊ถ๊ธˆํ•˜์‹  ์ ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋‚จ๊ฒจ์ฃผ์„ธ์š”. ๐Ÿค“

์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.


๐Ÿ“ ์ฐธ๊ณ  ์ž๋ฃŒ

Swift ๊ณต์‹ ๋ฌธ์„œ - The Basics

๊ณต์‹ ๋ฌธ์„œ (ํ•œ๊ธ€ ๋ฒ„์ „) ๊ธฐ๋ณธ (The Basics)

https://didu-story.tistory.com/145

https://icksw.tistory.com/2