μμμ λ³μ (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μ ν λΉ ν μ μλ€.
μ΅μ λμ μΈλ©ν νλ λ°©λ²
! κ°μ μΈλ©ν
var name: String? = "Steve" print(name) // Optional("Steve") print(name!) // Steveif λ¬Έ μ¬μ©
if name != nil { print(name) }μ΅μ λ λ°μΈλ© μ¬μ©
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λ³μλ λ©μλ λ΄μμ μ§μμμμ²λΌ μ¬μ© κ°λ₯. }
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 |