πŸ¦… Swift

The Basics

KiwiπŸ’» 2022. 5. 15. 23:52

μƒμˆ˜μ™€ λ³€μˆ˜ (Constants and Variables)

μƒμˆ˜(let)와 λ³€μˆ˜(var)λŠ” 이름(name)κ³Ό νŠΉμ • νƒ€μž…("kiwi": String)의 값을 μ—°κ²°ν•œλ‹€.

let name = "kiwi"
var name = "kiwi"

μƒμˆ˜λŠ” 처음 값을 μ„€μ •ν•΄ μ£Όλ©΄ 변경이 λΆˆκ°€λŠ₯ ν•˜μ§€λ§Œ, λ³€μˆ˜λŠ” λ‹€λ₯Έ κ°’μœΌλ‘œ 변경이 κ°€λŠ₯ ν•˜λ‹€.

NOTE❗️
기본적으둜 μƒμˆ˜(let)둜 선언을 ν•΄μ€€λ‹€. λ³€μˆ˜λŠ” 변경이 ν•„μš”ν•  λ•Œλ§Œ ν•΄μ€€λ‹€. (μ»΄νŒŒμΌλŸ¬κ°€ μƒμˆ˜λ‘œ μ„ μ–Έλœ 경우 λ³„λ„μ˜ μ΅œμ ν™”λ₯Ό μ§„ν–‰ν•˜μ—¬, 처리 속도가 빨라짐)

νƒ€μž… λͺ…μ‹œ(Type Annotations)

μƒμˆ˜ λ˜λŠ” λ³€μˆ˜λ₯Ό μ„ μ–Έν•  λ•Œ μ €μž₯ν•  수 μžˆλŠ” κ°’μ˜ μ’…λ₯˜λ₯Ό λͺ…ν™•νžˆ ν•˜κΈ° μœ„ν•΄ νƒ€μž… λͺ…μ‹œλ₯Ό ν•  수 μžˆλ‹€. ν•˜μ§€λ§Œ μƒμˆ˜ λ˜λŠ” λ³€μˆ˜λ₯Ό μ„ μ–Έν•  λ‹Ήμ‹œμ— μ΄ˆκΈ°κ°’μ„ μ§€μ •ν•œλ‹€λ©΄ νƒ€μž…μ„ μœ μΆ”ν•  수 있기 λ•Œλ¬Έμ— νƒ€μž… λͺ…μ‹œκ°€ ν•„μš”ν•œ κ²½μš°κ°€ λ“œλ¬Όλ‹€.

μƒμˆ˜μ™€ λ³€μˆ˜μ˜ 이름 (Naming Constants and Variables)

μƒμˆ˜μ™€ λ³€μˆ˜ 이름은 μœ λ‹ˆμ½”λ“œλ¬Έμžλ₯Ό ν¬ν•¨ν•˜μ—¬ λŒ€λΆ€λΆ„μ˜ 문자λ₯Ό 포함 ν•  수 μžˆλ‹€. κ·ΈλŸ¬λ‚˜ 숫자둜 μ‹œμž‘ν•˜λŠ” 이름은 선언이 λΆˆκ°€ν•˜λ‹€. λ˜ν•œ νŠΉμ • νƒ€μž…μœΌλ‘œ 이미 μƒμˆ˜ λ˜λŠ” λ³€μˆ˜λ₯Ό μ„ μ–Έν•˜μ˜€λ‹€λ©΄ λ™μΌν•œ μ΄λ¦„μœΌλ‘œλŠ” λ‹€μ‹œ 선언이 λΆˆκ°€ν•˜λ‹€.

NOTE❗️
Swift ν‚€μ›Œλ“œμ™€ λ™μΌν•œ μ΄λ¦„μœΌλ‘œ μƒμˆ˜ λ˜λŠ” λ³€μˆ˜λ₯Ό μ„ μ–Έν•΄μ•Ό ν•œλ‹€λ©΄, 이름을 λ°±ν‹±(`)으둜 λ¬Άμ–΄μ•Ό ν•œλ‹€. κ·ΈλŸ¬λ‚˜ μ™ λ§Œν•˜λ©΄ μ΄λŸ¬ν•œ 경우λ₯Ό λ§Œλ“€μ§€ 말자

νƒ€μž… 세이프티와 νƒ€μž… μœ μΆ” (Type Safety and Type Inference)

  • type-safe
    SwiftλŠ” type-safe언어이닀. κ·Έλ ‡κΈ° λ•Œλ¬Έμ— μ½”λ“œλ₯Ό 컴파일 ν•  λ•Œ νƒ€μž… 검사λ₯Ό μˆ˜ν–‰ν•˜κ³  μΌμΉ˜ν•˜μ§€ μ•ŠλŠ” νƒ€μž…μ„ 였λ₯˜λ‘œ ν‘œμ‹œν•œλ‹€. μ΄λŠ” 개발 κ³Όμ •μ—μ„œ 였λ₯˜λ₯Ό 빨리 ν¬μ°©ν•˜κ³  μˆ˜μ •μ„ κ°€λŠ₯μΌ€ ν•΄μ€€λ‹€.
  • νƒ€μž… μœ μΆ”
    ν•„μš”ν•œ κ°’μ˜ νŠΉμ • νƒ€μž…μ„ μ§€μ •ν•˜μ§€ μ•ŠμœΌλ©΄ SwiftλŠ” μ μ ˆν•œ νƒ€μž…μœΌλ‘œ μœ μΆ”λ₯Ό ν•œλ‹€.

NOTE❗️
μ†Œμˆ˜μ μ„ ν¬ν•¨ν•œ 수의 경우 Float이 μ•„λ‹ˆλΌ Double을 κΈ°λ³Έκ°’μœΌλ‘œ ν•œλ‹€.

νƒ€μž… 별칭 (Type Aliases)

typealias ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜μ—¬ 기쑴의 νƒ€μž…μ„ μ‚¬μš©μžκ°€ μ›ν•˜λŠ” λŒ€λ‘œ μ„ μ–Έ ν•  μˆ˜κ°€ μžˆλ‹€.

νŠœν”Œ (Tuples)

νŠœν”Œμ€ μ—¬λŸ¬κ°’μ„ ν•˜λ‚˜μ˜ κ°’μœΌλ‘œ κ·Έλ£Ήν™” ν•œ 것이닀. νŠœν”Œμ•ˆμ—λŠ” μ–΄λ– ν•œ νƒ€μž…λ„ 선언이 κ°€λŠ₯ν•˜λ©° κΌ­ 같은 값일 ν•„μš”λ„ μ—†λ‹€.

let http404Error = (404, "Not Found")
// http404Error is of type (Int, String), and equals (404, "Not Found")

μœ„μ— μ˜ˆμ‹œλŠ” νŠœν”Œμ„ μ΄μš©ν•˜μ—¬ μˆ«μžμ™€ μ‚¬λžŒμ΄ 읽을 수 μžˆλŠ” μ„€λͺ…을 제곡 ν•˜κ³  μžˆλ‹€. 이 경우 νŠœν”Œμ˜ νƒ€μž…μ€ (Int, String)이라고 μ„€λͺ… ν•  수 μžˆλ‹€.

  • νŠœν”Œμ˜ λ‚΄μš©μ„ λ³„λ„μ˜ μƒμˆ˜ λ˜λŠ” λ³€μˆ˜λ‘œ λΆ„ν•΄ν•˜μ—¬ μ‚¬μš© ν•  수 μžˆλ‹€.

    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"
  • νŠœν”Œμ˜ κ°’ 쀑 μΌλΆ€λ§Œ ν•„μš”ν•œ 경우 νŠœν”Œμ„ λΆ„ν•΄ν•  λ•Œ 밑쀄(_)을 μ‚¬μš©ν•΄ 쀄 수 μžˆλ‹€.

    let (justTheStatusCode, _) = http404Error
    print("The status code is \(justTheStatusCode)")
    // Prints "The status code is 404"
  • 인덱슀 값을 μ΄μš©ν•˜μ—¬ 값에 μ ‘κ·Ό ν•  수 μžˆλ‹€.

    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"

⭐️ νŠœν”Œμ€ ν•¨μˆ˜μ˜ λ°˜ν™˜ κ°’μœΌλ‘œ 특히 μœ μš©ν•˜κ²Œ 쓰일 수 μžˆλ‹€.

μ˜΅μ…”λ„ (Optionals)

값이 μ—†λŠ” 경우λ₯Ό λŒ€λΉ„ν•˜μ—¬ μ˜΅μ…”λ„μ„ μ‚¬μš©ν•  수 μžˆλ‹€. κ·ΈλŸ¬λ―€λ‘œ μ˜΅μ…”λ„μ€ 두 κ°€μ§€ κ°€λŠ₯성을 λ‚˜νƒ€λ‚Έλ‹€. 값이 μžˆλŠ” κ²½μš°μ—λŠ” μ˜΅μ…”λ„ 값을 벗겨낸 이후 값에 μ ‘κ·Όν•΄μ•Ό ν•œλ‹€

예λ₯Ό λ“€μ–΄ λ¬Έμžμ—΄ 값을 μ •μˆ˜ν˜•μœΌλ‘œ λ³€ν™˜ ν•˜λ €λŠ” 경우 λͺ¨λ“  λ¬Έμžμ—΄μ΄ μ •μˆ˜ κ°’μœΌλ‘œ λ³€ν™˜μ΄ κ°€λŠ₯ ν•œ 것은 μ•„λ‹ˆλ‹€. λ¬Έμžμ—΄ "123"은 μ •μˆ˜λ‘œ λ³€ν™˜μ΄ κ°€λŠ₯ν•˜λ‚˜ "hello, world"λŠ” λ³€ν™˜μ΄ λΆˆκ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ΄λ‹€.

μ•„λž˜μ˜ μ˜ˆμ‹œλ₯Ό 보면 String 값을 Int둜 μ΄ˆκΈ°ν™” ν•˜λ €λŠ” 것을 λ³Ό 수 μžˆλ‹€.

let possibleNumber = "123"
let convertedNumber = Int(possibleNumber)
// convertedNumber is inferred to be of type "Int?", or "optional Int"

이 경우 μœ„μ—μ„œ μ–ΈκΈ‰ ν•œ 것 처럼 λͺ¨λ“  λ¬Έμžμ—΄ 값이 μ •μˆ˜ν˜•μœΌλ‘œ λ³€ν™˜ ν•  수 μ—†κΈ° λ•Œλ¬Έμ—, Intκ°€ μ•„λ‹Œ Int의 값이 μžˆμ„ μˆ˜λ„ 있고 없을 μˆ˜λ„ μžˆλ‹€λŠ” 것을 λ‚˜νƒ€λ‚΄λŠ” μ˜΅μ…”λ„Intκ°’μœΌλ‘œ λ°˜ν™˜ν•œλ‹€.

nil

μ˜΅μ…”λ„ λ³€μˆ˜μ— νŠΉμˆ˜ν•œ 값인 nil을 ν• λ‹Ήν•˜μ—¬ 값이 μ—†λŠ” μƒνƒœλ₯Ό λ‚˜νƒ€λ‚Ό 수 μžˆλ‹€.

NOTE❗️
μ˜΅μ…”λ„μ΄ μ•„λ‹Œ μƒμˆ˜μ™€ λ³€μˆ˜μ—λŠ” nil을 ν• λ‹Ή ν•  수 μ—†λ‹€.

μ˜΅μ…”λ„μ„ μ–Έλž©ν•‘ ν•˜λŠ” 방법

  1. ! κ°•μ œ μ–Έλž©ν•‘

    var name: String? = "Steve"
    print(name) // Optional("Steve")  
    print(name!) // Steve
  2. if λ¬Έ μ‚¬μš©

     if name != nil { print(name) } 
  3. μ˜΅μ…”λ„ 바인딩 μ‚¬μš©

  • if let

    var name: String? = "Steve"
    if let nameTest = name {  
    print(nameTest)  
    } else {  
    print("nil μž…λ‹ˆλ‹€")  
    }
  • guard let

      func printName(){
        var value:String?
        value = "steve"
        print(value) // Optional("Lena")
        guard let name = value else { return }
        print(name) // "Lena"
    //nameλ³€μˆ˜λŠ” λ©”μ†Œλ“œ λ‚΄μ—μ„œ μ§€μ—­μƒμˆ˜μ²˜λŸΌ μ‚¬μš© κ°€λŠ₯.
      }
  1. Nil-Coalescing

    var name: String? = "Steve"
    let secondName = name ?? "Jobs"  
    print(secondName)

'πŸ¦… Swift' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

Functions  (0) 2022.06.01
Control Flow  (0) 2022.06.01
Collection Types  (0) 2022.05.22
Strings and Characters  (0) 2022.05.22
BasicΒ Operators  (0) 2022.05.15