# Swift: The Basics

yxnsxยท2021๋ 4์ 14์ผ
0

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

## ๐ฅ 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)")

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)")

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) {
}
• ์์ ์์์ 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