Swift: The Basics

yxnsxยท2021๋…„ 4์›” 14์ผ
0

Lang: Swift

๋ชฉ๋ก ๋ณด๊ธฐ
1/1
post-thumbnail

๐Ÿ”ฅ Swift - The Basics



โœจ Constants and Variables

  • ์ƒ์ˆ˜๋Š” let ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•˜๊ณ , ๋ณ€์ˆ˜๋Š” var ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ๋‹ค.
  • ์ƒ์ˆ˜์˜ ๊ฐ’์€ ํ•œ ๋ฒˆ ์„ค์ •๋˜๋ฉด ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†์ง€๋งŒ, ๋ณ€์ˆ˜๋Š” ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ƒ์ˆ˜์™€ ๋ณ€์ˆ˜๋Š” ์‚ฌ์šฉํ•˜๊ธฐ ์ „, ์„ ์–ธ์ด ์šฐ์„ ๋˜์–ด์•ผ ํ•œ๋‹ค.
let maximumNumberOfLoginAttempts = 10 // ์ƒ์ˆ˜
var currentLoginAttempt = 0 // ๋ณ€์ˆ˜

var x = 0.0, y = 0.0, z = 0.0 // ์—ฌ๋Ÿฌ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ํ•œ ์ค„์— ์„ ์–ธํ•  ์ˆ˜ ์žˆ๋‹ค.


โœจ Type Annotations

  • ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๊ฐ€ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ’์˜ ์œ ํ˜•์„ ๋ช…ํ™•ํžˆํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค.
  • ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜ ์ด๋ฆ„ ๋’ค์— ์ฝœ๋ก , ๊ณต๋ฐฑ, ํƒ€์ž…์„ ์ฐจ๋ก€๋กœ ์„ ์–ธํ•œ๋‹ค.
  • ์ดˆ๊ธฐ๊ฐ’์ด ์ง€์ •๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ, Type Annotations์œผ๋กœ ํƒ€์ž…์„ ์„ ์–ธํ•œ๋‹ค.
  • ์ดˆ๊ธฐ๊ฐ’์ด ์ง€์ •๋œ ๊ฒฝ์šฐ, Type Annotations์œผ๋กœ ํƒ€์ž…์ด ์„ ์–ธ๋˜์–ด ์žˆ์ง€ ์•Š์•„๋„ ํƒ€์ž… ์ถ”๋ก ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
var welcomeMessage: String
var red, green, blue: Double // ํ•œ ์ค„์— ์„ ์–ธํ•œ ๋ณ€์ˆ˜๋“ค์— ๋™์ผํ•œ ์œ ํ˜•์˜ ํƒ€์ž…์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๋‹ค.


โœจ Naming Constants and Variables

  • ์ƒ์ˆ˜ ๋ฐ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์€ ์œ ๋‹ˆ์ฝ”๋“œ ๋ฌธ์ž๋ฅผ ํฌํ•จํ•œ ๊ฑฐ์˜ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.
let ฯ€ = 3.14159    // ๊ฐ€๋Šฅ
let ไฝ ๅฅฝ = "ไฝ ๅฅฝไธ–็•Œ" // ๊ฐ€๋Šฅ
let ๐Ÿถ๐Ÿฎ = "dogcow" // ๊ฐ€๋Šฅ

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

  • ์ƒ์ˆ˜ ๋ฐ ๋ณ€์ˆ˜ ์ด๋ฆ„์ด ์ˆซ์ž๋กœ ์‹œ์ž‘๋˜์–ด์„œ๋Š” ์•ˆ๋œ๋‹ค.
let 1one = 1 // ๋ถˆ๊ฐ€๋Šฅ
let one1 = 1 // ๊ฐ€๋Šฅ

  • where, func ๋“ฑ ์Šค์œ„ํ”„ํŠธ ํ‚ค์›Œ๋“œ์™€ ๋™์ผํ•œ ์ด๋ฆ„์œผ๋กœ ์ƒ์ˆ˜ ๋ฐ ๋ณ€์ˆ˜์˜ ์ด๋ฆ„์„ ์ง€์ •ํ•ด์•ผ ํ•  ๊ฒฝ์šฐ, ๋ฐฑํ‹ฑ(`)์œผ๋กœ ๋ฌถ์–ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.
let where = "where"   // ๋ถˆ๊ฐ€๋Šฅ
let `where` = "where" // ๊ฐ€๋Šฅ


โœจ Comment

  • ํ•œ ์ค„ ์ฃผ์„์€ ๋‘ ๊ฐœ์˜ ์Šฌ๋ž˜์‹œ(//)๋กœ ์‹œ์ž‘ํ•œ๋‹ค.
// This is a comment.

  • ์—ฌ๋Ÿฌ ์ค„ ์ฃผ์„์€ ์Šฌ๋ž˜์‹œ์™€ ๋ณ„ํ‘œ(/*)๋กœ ์‹œ์ž‘ํ•˜๊ณ  ๋ณ„ํ‘œ์™€ ์Šฌ๋ž˜์‹œ(*/)๋กœ ๋๋‚œ๋‹ค.
/* This is also a comment
but is written over multiple lines. */

  • ์—ฌ๋Ÿฌ ์ค„ ์ฃผ์„์€ ์ค‘์ฒฉ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
/* 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. */


โœจ Semicolons

  • Swift๋Š” ์ฝ”๋“œ์˜ ๊ฐ ๋ช…๋ น๋ฌธ ๋’ค์— ์„ธ๋ฏธ์ฝœ๋ก (;)์„ ์ž‘์„ฑํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • ๋‹ค๋งŒ, ์•„๋ž˜์™€ ๊ฐ™์ด ํ•œ ์ค„์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๊ฐœ๋ณ„๋ฌธ์„ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์„ธ๋ฏธ์ฝœ๋ก ์ด ํ•„์š”ํ•˜๋‹ค.
let cat = "๐Ÿฑ"; print(cat)


โœจ Integers

โœ”๏ธ Integer Bounds

  • Swift๋Š” 8, 16, 32, 64-bit ํ˜•์‹์œผ๋กœ ์ •์ˆ˜๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
  • min ๋ฐ max ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์ •์ˆ˜ ์œ ํ˜•์˜ ์ตœ์†Ÿ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
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

  • ๋ช…์‹œ์ ์œผ๋กœ ํฌ๊ธฐ๊ฐ€ ์ง€์ •๋œ ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ ๋˜๋Š” ์„ฑ๋Šฅ, ๋ฉ”๋ชจ๋ฆฌ ์‚ฌ์šฉ๋Ÿ‰ ๋“ฑ ์ตœ์ ํ™”๋ฅผ ์œ„ํ•œ ์ž‘์—… ๋“ฑ ํŠน๋ณ„ํ•œ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด Int ์ •์ˆ˜๊ฐ’์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • 32-bit ํ”Œ๋žซํผ์—์„œ์˜ Int size = Int32
  • 64-bit ํ”Œ๋žซํผ์—์„œ์˜ Int size = Int64

โœ”๏ธ UInt

  • ๋ถ€ํ˜ธ ์—†๋Š” ์ •์ˆ˜ ์œ ํ˜•(0, ์–‘์ˆ˜)๋งŒ ์ œ๊ณตํ•œ๋‹ค.
  • 32-bit ํ”Œ๋žซํผ์—์„œ์˜ UInt size = UInt32
  • 64-bit ํ”Œ๋žซํผ์—์„œ์˜ UInt size = UInt64


โœจ Floating-Point Numbers

  • ๋ถ€๋™ ์†Œ์ˆ˜์  ํƒ€์ž…์€ ์ •์ˆ˜ ํƒ€์ž…๋ณด๋‹ค ํ›จ์”ฌ ๋” ๋„“์€ ๋ฒ”์œ„์˜ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
  • Swift๋Š” ๋‘ ์ข…๋ฅ˜์˜ ๋ถ€๋™ ์†Œ์ˆ˜์  ํƒ€์ž…์„ ์ œ๊ณตํ•œ๋‹ค.
    • 32-bit ๋ถ€๋™ ์†Œ์ˆ˜์  ์ˆซ์ž = Float
    • 64-bit ๋ถ€๋™ ์†Œ์ˆ˜์  ์ˆซ์ž = Double


โœจ Numeric Literals

  • ์ •์ˆ˜ ๋ฆฌํ„ฐ๋Ÿด์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
    • 10์ง„๋ฒ• ์ˆซ์ž - ์ ‘๋‘์–ด ํ•„์š” ์—†์Œ
    • 2์ง„๋ฒ• ์ˆซ์ž - 0b ์ ‘๋‘์–ด ํ•„์š”
    • 8์ง„๋ฒ• ์ˆซ์ž - 0o ์ ‘๋‘์–ด ํ•„์š”
    • 16์ง„๋ฒ• ์ˆซ์ž - 0x ์ ‘๋‘์–ด ํ•„์š”
let decimalInteger = 17
let binaryInteger = 0b10001   // 2์ง„๋ฒ•์œผ๋กœ ๋‚˜ํƒ€๋‚ธ 17
let octalInteger = 0o21       // 8์ง„๋ฒ•์œผ๋กœ ๋‚˜ํƒ€๋‚ธ 17
let hexadecimalInteger = 0x11 // 16์ง„๋ฒ•์œผ๋กœ ๋‚˜ํƒ€๋‚ธ 17

  • ๋ถ€๋™ ์†Œ์ˆ˜์  ๋ฆฌํ„ฐ๋Ÿด์€ ์ง€์ˆ˜๋ฅผ ์ง€๋‹ ์ˆ˜ ์žˆ๋‹ค.
    • 10์ง„๋ฒ• ์ˆซ์ž (10exp์”ฉ ๊ณฑํ•จ)
      • 1.25e2 = 1.25 x 102 = 125.0
      • 1.25e-2 = 1.25 x 10-2 = 0.0125
    • 2์ง„๋ฒ• ์ˆซ์ž (2exp์”ฉ ๊ณฑํ•จ)
      • 0xFp2 = 15 x 22 = 60.0
      • 0xFp-2 = 15 x 2-2 = 3.75
let decimalDouble = 12.1875     // = 12.1875
let exponentDouble = 1.21875e1  // = 12.1875
let hexadecimalDouble = 0xC.3p0 // = 12.1875

  • ์ˆซ์ž ๋ฆฌํ„ฐ๋Ÿด์€ ์‰ฝ๊ฒŒ ์ฝ์„ ์ˆ˜ ์žˆ๋„๋ก ์ถ”๊ฐ€์ ์ธ ํ˜•์‹์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ •์ˆ˜์™€ ๋ถ€๋™ ์†Œ์ˆ˜์  ๋ชจ๋‘ 0์œผ๋กœ ์ฑ„์šธ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ฐ‘์ค„์„ ํฌํ•จํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋Ÿฌํ•œ ํ˜•์‹์€ ๋ฆฌํ„ฐ๋Ÿด์˜ ๊ธฐ๋ณธ ๊ฐ’์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š๋Š”๋‹ค.
let paddedDouble = 000123.456
let oneMillion = 1_000_000
let justOverOneMillion = 1_000_000.000_000_1


โœจ Numeric Type Conversion

โœ”๏ธ Integer Conversion

  • ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ์ˆซ์ž์˜ ๋ฒ”์œ„๋Š” ํƒ€์ž…๋งˆ๋‹ค ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ํƒ€์ž… ๋ณ€ํ™˜์ด ํ•„์š”ํ•˜๋‹ค.
let twoThousand: UInt16 = 2_000
let one: UInt8 = 1
let twoThousandAndOne = twoThousand + UInt16(one)

โœ”๏ธ Integer and Floating-Point Conversion

let three = 3
let pointOneFourOneFiveNine = 0.14159
let pi = Double(three) + pointOneFourOneFiveNine // Int -> Double ๋ณ€ํ™˜
let integerPi = Int(pi) // Double -> Int ๋ณ€ํ™˜


โœจ Type Aliases

  • ํƒ€์ž… ๋ณ„์นญ์€ ๊ธฐ์กด ํƒ€์ž…์˜ ๋Œ€์ฒด ์ด๋ฆ„์„ ์ •์˜ํ•œ๋‹ค.
  • ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋” ์ ์ ˆํ•œ ์ด๋ฆ„์œผ๋กœ ๊ธฐ์กด ์œ ํ˜•์„ ์ฐธ์กฐํ•˜๋Š” ๊ฒฝ์šฐ ์œ ์šฉํ•˜๋‹ค.
  • ํƒ€์ž… ๋ณ„์นญ์„ ์ •์˜ํ•˜๋ฉด ์›๋ž˜์˜ ์ด๋ฆ„์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ณณ์—์„œ ๋ณ„์นญ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
typealias AudioSample = UInt16
var maxAmplitudeFound = AudioSample.min


โœจ Booleans

  • Boolean ๊ฐ’์€ true ๋˜๋Š” false๋งŒ ๋  ์ˆ˜ ์žˆ๋Š” logical ๊ฐ’์ด๋‹ค.
  • ์ด๋Ÿฌํ•œ Boolean ๊ฐ’์€ if์™€ ๊ฐ™์€ ์กฐ๊ฑด๋ฌธ์œผ๋กœ ์ž‘์—…ํ•  ๋•Œ ํŠนํžˆ ์œ ์šฉํ•˜๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
let orangesAreOrange = true
let turnipsAreDelicious = false

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


โœจ Tuples

  • ํŠœํ”Œ์€ ์—ฌ๋Ÿฌ ๊ฐ’์„ ๋‹จ์ผ ๋ณตํ•ฉ ๊ฐ’์œผ๋กœ ๊ทธ๋ฃนํ™”ํ•œ๋‹ค.
  • ํŠœํ”Œ์€ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์—ฌ๋Ÿฌ ๊ฐœ์ธ ํ•จ์ˆ˜์—์„œ ํŠนํžˆ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํŠœํ”Œ ๋‚ด์˜ ๊ฐ’์€ ๋ชจ๋“  ์œ ํ˜•์ด ๋  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์„œ๋กœ ๋™์ผํ•œ ์œ ํ˜•์ผ ํ•„์š”๋Š” ์—†๋‹ค.
let http404Error = (404, "Not Found")
// http404Error is of type (Int, String), and equals (404, "Not Found")

  • ํŠœํ”Œ ๋‚ด์˜ ๊ฐ’์€ ๊ฐ๊ฐ์˜ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๋กœ ๋ถ„ํ•ดํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ๊ฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
let (statusCode, statusMessage) = http404Error

// Prints "The status code is 404"
print("The status code is \(statusCode)")

// Prints "The status message is Not Found"
print("The status message is \(statusMessage)")

  • ํŠœํ”Œ ๋‚ด์˜ ๊ฐ’ ์ค‘ ์ผ๋ถ€๋งŒ ํ•„์š”ํ•  ๊ฒฝ์šฐ, ์–ธ๋”์Šค์ฝ”์–ด(_)๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ถˆํ•„์š”ํ•œ ๋ถ€๋ถ„์„ ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ๋‹ค.
let (justTheStatusCode, _) = http404Error

// Prints "The status code is 404"
print("The status code is \(justTheStatusCode)")

  • 0๋ถ€ํ„ฐ ์‹œ์ž‘ํ•˜๋Š” ์ธ๋ฑ์Šค๋ฅผ ํ†ตํ•ด ํŠœํ”Œ ๋‚ด์˜ ๊ฐ ๊ฐ’์— ์ ‘๊ทผํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
// Prints "The status code is 404"
print("The status code is \(http404Error.0)")

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

  • ํŠœํ”Œ์„ ์ •์˜ํ•  ์‹œ, ํŠœํ”Œ ๋‚ด์˜ ๊ฐ ๊ฐ’์— ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํŠœํ”Œ ๋‚ด์˜ ๊ฐ ๊ฐ’์— ์ด๋ฆ„์„ ์ง€์ •ํ•œ ๊ฒฝ์šฐ, ํ•ด๋‹น ๊ฐ’์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด ์ง€์ •ํ•œ ์ด๋ฆ„์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
let http200Status = (statusCode: 200, description: "OK")

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

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


โœจ Optionals

  • optional์€ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๊ฐ€ "๊ฐ’์ด ์—†์Œ"์„ ์ง€๋‹ ์ˆ˜ ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
  • optional์€ ๋‘ ๊ฐ€์ง€ ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
    • ๊ฐ’์ด ์žˆ๊ณ , ๊ทธ ๊ฐ’์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•ด optional์„ ํ•ด์ œํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ
    • ๊ฐ’์ด ์—†๋Š” ์ƒํƒœ
  • ์Šค์œ„ํ”„ํŠธ์˜ optional์€ ํŠน๋ณ„ํ•œ ์ƒ์ˆ˜ ์—†์ด ์–ด๋–ค ํƒ€์ž…์—๋„ ๊ฐ’์ด ์—†์Œ์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๊ฒŒ ํ•œ๋‹ค.

let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
// convertedNumber is inferred to be of type "Int?", or "optional Int"
  • ์œ„์˜ ์˜ˆ์‹œ์—์„œ possibleNumber์˜ ๊ฐ’์œผ๋กœ "Hello, world"๋“ฑ์ด ์ง€์ •๋  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด ๋•Œ, convertedNumber์—์„œ์˜ Int(possibleNumber) ๋ณ€ํ™˜์€ ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋Ÿฌํ•œ ๊ฒฝ์šฐ๋ฅผ ์œ„ํ•ด convertedNumber์˜ ๊ฐ’์€ Int? ๋˜๋Š” optional Int๋กœ ์ถ”๋ก ๋œ๋‹ค.

โœ”๏ธ nil

  • ๊ฐ’์ด ์—†๋Š” ์ƒํƒœ์— nil์ด๋ผ๋Š” ํŠน์ •ํ•œ ๊ฐ’์„ ํ• ๋‹นํ•˜์—ฌ optional ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ธฐ๋ณธ๊ฐ’์„ ์ง€์ •ํ•˜์ง€ ์•Š๊ณ  optional ๋ณ€์ˆ˜๋ฅผ ์ •์˜ํ•  ๊ฒฝ์šฐ, ๋ณ€์ˆ˜๋Š” ์ž๋™์œผ๋กœ nil ๊ฐ’์œผ๋กœ ์„ค์ •๋œ๋‹ค.
// serverResponseCode contains an actual Int value of 404
var serverResponseCode: Int? = 404

// serverResponseCode now contains no value
serverResponseCode = nil

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

โœ”๏ธ If Statements and Forced Unwrapping

  • if๋ฌธ์œผ๋กœ optional๊ณผ nil ๊ฐ’์„ ๋น„๊ตํ•ด์„œ optional์ด ๊ฐ’์„ ์ง€๋‹ˆ๊ณ  ์žˆ๋Š”์ง€ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
if convertedNumber != nil {
    print("convertedNumber has an integer value of \(convertedNumber!).")
}
// Prints "convertedNumber has an integer value of 123."

โœ”๏ธ Optional Binding

  • optional binding์„ ํ†ตํ•ด optional์ด ๊ฐ’์„ ์ง€๋‹ˆ๊ณ  ์žˆ๋Š”์ง€ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฐ’์ด ์กด์žฌํ•  ๊ฒฝ์šฐ, ํ•ด๋‹น ๊ฐ’์„ ์ƒ์ˆ˜ ๋˜๋Š” ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
if let actualNumber = Int(possibleNumber) {
    print("The string \"\(possibleNumber)\" has an integer value of \(actualNumber)")
} else {
    print("The string \"\(possibleNumber)\" couldn't be converted to an integer")
}
// Prints "The string "123" has an integer value of 123"

โœ”๏ธ Implicitly Unwrapped Optionals

  • optional ๊ฐ’์ด ํ•ญ์ƒ ๊ฐ’์„ ์ง€๋‹ˆ๊ณ  ์žˆ๋‹ค๊ณ  ๊ฐ€์ •ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ, ํ•ด๋‹น ๊ฐ’์— ์•ก์„ธ์Šค ํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ’์˜ ์œ ๋ฌด๋ฅผ ํ™•์ธํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • optional๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜ ์‚ฌ์šฉ์‹œ, ํ•ด๋‹น ๋ณ€์ˆ˜๋ช… ๋’ค์— ๋Š๋‚Œํ‘œ๋ฅผ ๋„ฃ์–ด ํ•ญ์ƒ ๊ฐ’์„ ์ง€๋‹ˆ๊ณ  ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
  • ํ•ด๋‹น ๋ณ€์ˆ˜ ์„ ์–ธ์‹œ, ์ž๋ฃŒํ˜• ๋’ค์— ๋Š๋‚Œํ‘œ๋ฅผ ๋„ฃ์–ด ํ•ญ์ƒ ๊ฐ’์„ ์ง€๋‹ˆ๊ณ  ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
let possibleString: String? = "An optional string."
let forcedString: String = possibleString! // requires an exclamation point

let assumedString: String! = "An implicitly unwrapped optional string."
let implicitString: String = assumedString // no need for an exclamation point


โœจ Error Handling

  • ์‹คํ–‰ ์ค‘ ํ”„๋กœ๊ทธ๋žจ์—์„œ ๋‚˜ํƒ€๋‚˜๋Š” ๋ฌธ์ œ์˜ ์—๋Ÿฌ ์ƒํ™ฉ์— ๋”ฐ๋ผ ์‘๋‹ตํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.
  • ์—๋Ÿฌ ์ฒ˜๋ฆฌ๋Š” ๋‚ด์žฌํ•˜๋Š” ์‹คํŒจ ์›์ธ์„ ํŒ๋ณ„ํ•˜๊ณ , ํ•„์š”ํ•œ ๊ฒฝ์šฐ ํ”„๋กœ๊ทธ๋žจ์˜ ๋‹ค๋ฅธ ๋ถ€๋ถ„์œผ๋กœ ํ•ด๋‹น ์—๋Ÿฌ๋ฅผ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์—๋Ÿฌ ์ƒํ™ฉ์„ ๋งˆ์ฃผํ•œ ํ•จ์ˆ˜๋Š” ์—๋Ÿฌ๋ฅผ throwsํ•˜๊ณ , ํ•ด๋‹น ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ์ž๋Š” ์—๋Ÿฌ๋ฅผ ์žก์•„๋‚ด๊ณ  ์ ์ ˆํ•˜๊ฒŒ ์‘๋‹ตํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด๋Ÿฌํ•œ ํ•จ์ˆ˜๋Š” ์—๋Ÿฌ๋ฅผ throwsํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด throws ํ‚ค์›Œ๋“œ๋ฅผ ํฌํ•จํ•ด์•ผ ํ•œ๋‹ค.
func canThrowAnError() throws {
    // this function may or may not throw an error
}

  • swift๋Š” catch์ ˆ์— ์˜ํ•ด ์ฒ˜๋ฆฌ๋  ๋•Œ๊นŒ์ง€ ํ•ด๋‹น ๋ฒ”์œ„ ๋‚ด์—์„œ ์ž๋™์œผ๋กœ ์—๋Ÿฌ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.
  • do๋ฌธ์€ ์—๋Ÿฌ๋ฅผ ํ•˜๋‚˜ ์ด์ƒ์˜ catch์ ˆ๋กœ ์ „ํŒŒํ•˜๋Š” ์ƒˆ ๊ตฌ๊ฐ„์„ ์ƒ์„ฑํ•œ๋‹ค.
do {
    try canThrowAnError()
    // no error was thrown
} catch {
    // an error was thrown
}

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

do {
    try makeASandwich()
    eatASandwich()
} catch SandwichError.outOfCleanDishes {
    washDishes()
} catch SandwichError.missingIngredients(let ingredients) {
    buyGroceries(ingredients)
}
  • ์œ„์˜ ์˜ˆ์—์„œ makeASandwich() ํ•จ์ˆ˜๋Š” ๊นจ๋—ํ•œ ์ ‘์‹œ๊ฐ€ ์—†์„ ๊ฒฝ์šฐ ๋˜๋Š” ์žฌ๋ฃŒ๊ฐ€ ๋น ์กŒ์„ ๊ฒฝ์šฐ ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ๋‹ค.
  • ์•„๋ฌด๋Ÿฐ ์—๋Ÿฌ๋„ ๋ฐœ์ƒํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ,eatASandwich() ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
  • do๋ฌธ์œผ๋กœ ๊ฐ์‹ธ์ธ ํ•จ์ˆ˜ ํ˜ธ์ถœ์—์„œ๋Š” ์–ด๋– ํ•œ ์—๋Ÿฌ๋ผ๋„ catch์ ˆ๋กœ ์ „๋‹ฌ๋œ๋‹ค.
  • ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๊ณ  ํ•ด๋‹น ์—๋Ÿฌ๊ฐ€ SandwichError.outOfCleanDishes์™€ ์ผ์น˜ํ•  ๊ฒฝ์šฐ, washDishes() ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.
  • ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ–ˆ๊ณ  ํ•ด๋‹น ์—๋Ÿฌ๊ฐ€ SandwichError.missingIngredients์™€ ์ผ์น˜ํ•  ๊ฒฝ์šฐ, catch ํŒจํ„ด์— ์˜ํ•ด ์บก์ณ๋œ [String] ๊ฐ’๊ณผ ํ•จ๊ป˜ buyGroceries(_:) ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ๋‹ค.


โœจ Assertions and Preconditions

โœ”๏ธ Debugging with Assertions

  • swift์˜ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์—์„œ assert(_:_:file:line:)๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ assertion์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๊ฒฐ๊ณผ๊ฐ€ true์ผ ๊ฒฝ์šฐ, ์ฝ”๋“œ ์‹คํ–‰์ด ๊ณ„์†๋œ๋‹ค.
  • ๊ฒฐ๊ณผ๊ฐ€ false์ผ ๊ฒฝ์šฐ, assertion์— ์‹คํŒจํ•˜์—ฌ ํ”„๋กœ๊ทธ๋žจ์ด ์ข…๋ฃŒ๋œ๋‹ค.
  • 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(age >= 0)

  • ์กฐ๊ฑด์„ ์ด๋ฏธ ํ™•์ธํ•œ ๊ฒฝ์šฐ, assertionFailure(_:file:line:)ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ assertion์ด ์‹คํŒจํ–ˆ๋Š”์ง€ ์•Œ๋ฆด ์ˆ˜ ์žˆ๋‹ค.
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.")
}

โœ”๏ธ Enforcing Preconditions

  • ์กฐ๊ฑด์ด false๊ฐ€ ๋  ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์„ ๊ฒฝ์šฐ precondition์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์ฝ”๋“œ๊ฐ€ ๊ณ„์† ์‹คํ–‰๋˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ฐ˜๋“œ์‹œ true์—ฌ์•ผ ํ•œ๋‹ค.
  • ์–ด๋– ํ•œ ๊ฐ’์ด ๋ฒ”์œ„๋ฅผ ๋ฒ—์–ด๋‚˜์ง€๋Š” ์•Š๋Š”์ง€, ํ•จ์ˆ˜์— ์œ ํšจํ•œ ๊ฐ’์ด ์ „๋‹ฌ๋˜๋Š”์ง€๋ฅผ ์ฒดํฌํ•˜๊ธฐ ์œ„ํ•ด precondition์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • precondition(_:_:file:line:) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ precondition์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
  • true ๋˜๋Š” false๋ฅผ ํŒ๋ณ„ํ•  ์ˆ˜ ์žˆ๋Š” ํ‘œํ˜„์‹๊ณผ ๊ฒฐ๊ณผ๊ฐ€ false์ผ ๊ฒฝ์šฐ ๋ณด์—ฌ์งˆ ๋ฉ”์‹œ์ง€๋ฅผ ์ด ํ•จ์ˆ˜์— ์ „๋‹ฌํ•œ๋‹ค.
  • preconditionFailure(_:file:line:)ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ precondition์ด ์‹คํŒจํ–ˆ๋Š”์ง€ ์•Œ๋ฆด ์ˆ˜ ์žˆ๋‹ค.
let age = -3
assert(age >= 0, "A person's age can't be less than zero.")
// This assertion fails because -3 isn't >= 0.


๐Ÿ“ References

  • https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html

๊ด€์‹ฌ ์žˆ์„ ๋งŒํ•œ ํฌ์ŠคํŠธ

0๊ฐœ์˜ ๋Œ“๊ธ€