μ€μννΈμ κ½(?)π·μ΄λΌκ³ ν μ μλ μ΅μ λ(Optional)μ λͺ¨λ κ²μ λν΄ μμ보μ!
π»μ΅μ λ (Optional)π»
μ΅μ λμ΄λ?
κ°μ΄ μμ μλ μκ³ μμ μλ μλ κ²μ λνλΈλ€.
→ μ¬κΈ°μ κ°μ΄ μλ€λ κ²μ nilμ λνλΈλ€.
→ μ¦, μ΅μ λμ μ΄λ€ κ°(String, Int, ...) λλ nilμ κ°μ§κ³ μλ€.
→ λΉ λ°°μ΄μ΄λ λΉ λμ λ리λ λΉμ΄ μμ λΏμ΄μ§ κ°μ΄ μλ κ²μ μλλ€.
μ μνλ λ°©λ²μ?
Type λ€μ '?' λ₯Ό λΆμ΄λ©΄ λλ€. λ무 μ½λ€.β
var name: String?
μ΅μ λμ κΈ°λ³Έκ°μ nil μ΄λ€. μ¦, μ΅μ λμ μ΄κΉκ°μ μ νμ§ μμΌλ©΄ defultλ nilμ΄λΌλ λ§μ΄λ€.
var name: String?
print(name) // nil
name = "JINaPPLE"
print(name) // Optional("JINaPPLE")
μΌλ° λ³μμ λ€λ₯Έμ
optionalName μ μ΅μ λλ‘ μ μΈλ λ³μμ΄κΈ° λλ¬Έμ μ€μ μ½λκ° μ§νλκΈ° μ κΉμ§λ κ°μ΄ μμμ§ μμμ§ λͺ¨λ₯΄μ§λ§,
requiredName μ StringμΌλ‘ μ μΈλ λ³μμ΄κΈ° λλ¬Έμ νμ κ°μ κ°μ§κ³ μμ΄μΌνλ€.
λ°λΌμ Swiftλ μμ μ λλΉν΄ requredName μ optionalNameμ λμ ν μ μκ² λ§λ€μλ€.
let optionalName: String? = "JINaPPLE"
let requiredName: String = optionalName // ERROR !!
π»μ΅μ λ λ°μΈλ© (Optional Binding)π»
μ΅μ λμ κ°μ κ°μ Έμ€κ³ μΆλ€λ©΄? μ΅μ λ λ°μΈλ© μ¬μ©νκΈ°!!
μ΅μ λ λ°μΈλ©μ΄λ?
μ΅μ λμ κ°μ΄ μ‘΄μ¬νλμ§ κ²μ¬ν λ€, λ§μ½ μ΅μ λμ κ°μ΄ μ‘΄μ¬νλ€λ©΄ κ·Έ κ°μ λ€λ₯Έ λ³μμ λμ μμΌμ€λ€.
→ if let (μμ) λλ if var (λ³μ) μ¬μ©
→ μ΅μ λμ κ°μ΄ μλ€λ©΄ if λ¬Έ μμΌλ‘ λ€μ΄κ°κ³ , nilμ΄λΌλ©΄ ifλ¬Έ ν΅κ³Ό~ pass~
if let name = optionalName {
print(name) // optionalNameμ κ°μ΄ μλ€λ©΄, nameμ κ°μ λμ
ν ν ifλ¬Έ μμΌλ‘ λ€μ΄μμ print
}
// optionalNameμ΄ nil μ΄λΌλ©΄ ifλ¬Έ ν΅κ³Ό
μ΅μ λ λ°μΈλ©μ μ¬λ¬κ° νκ³ μΆλ€λ©΄?
μ½€λ§(,)λ‘ κ΅¬λΆνμ¬ μ΅μ λ λ°μΈλ©μ ν μ μλ€. λͺ¨λ μ΅μ λμ κ°μ΄ μ‘΄μ¬ν΄μΌ ifλ¬Έ μμΌλ‘ λ€μ΄μ¬ μ μλ€.
var optionalName: String? = "JINaPPLE"
var optionalEmail: String? = "JINaPPLE@apple.com"
if let name = optionalName, email = optionalEmail {
// nameκ³Ό email κ°μ΄ μ‘΄μ¬
}
μ΅μ λ λ°μΈλ©μμ 쑰건λ λ£μ μ μλ€κ³ ?
Yes. μ½€λ§λ₯Ό μ¬μ©ν΄μ.
μ½€λ§ μ΄νμ 쑰건μ μ μ΅μ λ λ°μΈλ©μ΄ μΌμ΄λ νμ μ€νλλ€. μ¦, μ΅μ λμ΄ λ²κ²¨μ§ κ°μ κ°μ§κ³ 쑰건μ κ²μ¬νλ€.
var optionalAge: Int? = 22
if let age = optionalAge, age >= 20 {
// ageμ κ°μ΄ μ‘΄μ¬νκ³ , 20 μ΄μ
}
π»μ΅μ λ 체μ΄λ (Optional Chaining)π»
swift μ½λλ₯Ό λμ± κ°κ²°νκ² λ§λ€μ΄μ£Όλ μμ΄π₯Ί
EX ) μ΅μ λλ‘ μ μΈλ λ°°μ΄μ΄ λΉ λ°°μ΄μΈμ§ μ΄λ»κ² κ²μ¬ν κΉ? -> nil μ΄ μλλ©΄μ λΉ λ°°μ΄μΈμ§ νμΈ!
let array: [String]? = []
var isEmptyArray = false
if let array = array, array.isEmpty {
isEmptyArray = true // array μ κ°μ΄ μκ³ , λΉμ΄ μμΌλ©΄ true
} else {
isEmptyArray = false // arrayμ κ°μ΄ μκ±°λ(nil), λΉμ΄ μμ§ μμΌλ©΄ false
}
μ μ½λλ μ΅μ λ 체μ΄λμ μ¬μ©νλ©΄ μλμ κ°μ΄ κ°λ¨νκ² μμ±ν μ μλ€.
let isEmptyArray = array?.isEmpty == true
μ΅μ λ 체μ΄λμ μ΅μ λμ μμ±μ μ κ·Όν λ, μ΅μ λ λ°μΈλ© κ³Όμ μ '?' ν€μλλ‘ μ€μ¬μ£Όλ μν μ νλ€.
μμ μν©μ μλμ 3κ°μ§μ κ²½μ°μ μλ₯Ό λ³μ μ μλ€.
βοΈ arrayκ° nil μΈ κ²½μ°
array?.isEmpty
~~~~~~
μ¬κΈ°κΉμ§ μ€νλκ³ `nil`μ λ°ν
βοΈ arrayκ° λΉ λ°°μ΄μΈ κ²½μ°
array?.isEmpty
~~~~~~~~~~~~~~
μ¬κΈ°κΉμ§ μ€νλκ³ `true`λ₯Ό λ°ν
βοΈarrayμ κ°μ΄ μλ κ²½μ°
array?.isEmpty
~~~~~~~~~~~~~~
μ¬κΈ°κΉμ§ μ€νλκ³ `false`λ₯Ό λ°ν
→ λ°λΌμ μμ κ²°κ³Όλ‘ κ°κ° nil, true, falseκ° λμ€κ² λκ³ , arrayκ° λΉ λ°°μ΄μΈ κ²½μ°λ₯Ό νλ¨ν΄μΌ νλ―λ‘ array?.isEmpty λ€μ == true λ₯Ό ν΄μ£Όμ΄μΌνλ€. (isEmptyμ λ°νκ°μ BoolμΈλ°, μ΅μ λ 체μ΄λμΌλ‘ μΈν΄ Bool?μ λ°ννκ² λλ€)
π»μ΅μ λ λ²κΈ°κΈ°π»
μ΅μ λμ μ¬μ©ν λλ§λ€ μ΅μ λ λ°μΈλ©μ μ¬μ©νμ¬ κ°μ κ°μ Έμ€λ κ²μ΄ λ°λμ§νλ€.
νμ§λ§, λΆλͺ ν κ°μ΄ μ‘΄μ¬ν¨μλ λΆκ΅¬νκ³ μ΅μ λλ‘ μ¬μ©ν΄μΌν λκ° μλ€.
μ΄λ΄ λμλ μ΅μ λμ κ°μ΄ μλ€κ³ κ°μ νκ³ κ°μ λ°λ‘ μ κ·Όν μ μλ '!'λ₯Ό λΆμ¬μ μ¬μ©νλ€.
print(optionalName) // Optional("JINaPPLE")
print(optionalName!) // JINaPPLE
β οΈ μ£Όμν μ β οΈ
μ΅μ λμ κ°μ΄ nilμΈ κ²½μ°μλ λ°νμ μλ¬κ° λ°μνλ€. ios μ±μ΄ κ°μ λ‘ μ’ λ£λλ μ‘°μ¬ν΄μ μ¬μ©ν΄μΌ νλ€!
var optionalEmail: String?
print(optionalEmail!) // λ°νμ μλ¬
//fatal error: unexpectedly found nil while unwrapping an Optional value
Reference: https://devxoul.gitbooks.io/ios-with-swift-in-40-hours/content/Chapter-2/optionals.html