- Optional Chainingμ νμ¬ nilμΌ κ°λ₯μ±μ΄ μλ μ΅μ λ νμ μ νλ‘νΌν°, λ©μλ, μλΈ μ€ν¬λ¦½νΈλ₯Ό 쿼리νκ³ νΈμΆνλ νλ‘μΈμ€
- κ°μ΄ μμΌλ©΄ νΈμΆμ μ±κ³΅; κ°μ΄ nilμ΄λ©΄ nil λ°ν
- μ¬λ¬ κ°μ 쿼리λ₯Ό μ°κ²°ν μ μμΌλ©°, νλλΌλ nilμ΄λ©΄ μ 체 체μΈμ΄ μ€ν¨νλ€.
- μ΅μ λ 체μ΄λμ κ° λ€μ λ¬Όμν(?)λ₯Ό λΆμ¬μ νν κ°λ₯
- μ΅μ λμ μ¬μ©ν μ μλ κ°: νλ‘νΌν°, λ©μλ, μλΈ μ€ν¬λ¦½νΈ
- κ°μ μΈλν(! μ¬μ©)κ³Ό λ¬Έλ² μ μ¬
- μ°¨μ΄μ : κ°μ΄ μμΌλ©΄ κ°μ μΈλνμ λ°νμ μλ¬κ° λ°μνμ§λ§, μ΅μ λ 체μ΄λμ nil λ°ν
- μ΅μ
λ 체μ΄λμ μν΄ nil κ°μ΄ νΈμΆλ μ μκΈ° λλ¬Έμ μ΅μ
λ 체μ΄λμ κ°μ νμ μ΅μ
λ
- μ΅μ λ κ°μ λ°ννμ§ μλ κ°(νλ‘νΌν°, λ©μλ, μλΈ μ€ν¬λ¦½νΈ)λ₯Ό νΈμΆνλλΌλ μ΅μ λ 체μ΄λμ μν΄ μ΅μ λ κ°μΌλ‘ λ°ν
- μ΅μ
λ 체μ΄λμ νΈμΆ κ²°κ³Όλ μμλλ λ°ν κ°κ³Ό λμΌν νμ
μ΄μ§λ§ μ΅μ
λλ‘ λνν¨
- ex) μμλλ κ²°κ³Όλ‘ Int λ°ν; μ΅μ λ 체μ΄λμ μ¬μ©νλ©΄ Int? λ°ν
- μ΅μ λ 체μ΄λμ ν λ λ²¨μ΄ μλ μ¬λ¬ λ λ²¨λ‘ μ¬μ©ν μ μλ€. (multilevel optional chaining)
- 볡μ‘ν ꡬ쑰μ λͺ¨λΈμμ νμ νλ‘νΌν°μ μ κ·Όν΄μ κ°μ΄ μλμ§ νμΈν μ μλ€.
- μ²΄μΈ μ€ νλλΌλ nilμ λ°ννλ©΄ μ€ν¨νλ€.
class Person {
var residence: Residence?
}
class Residence {
var rooms: [Room] = []
var numberOfRooms: Int {
return rooms.count
}
subscript(i: Int) -> Room {
get {
return rooms[i]
}
set {
rooms[i] = newValue
}
}
func printNumberOfRooms() {
print("The number of rooms is \(numberOfRooms)")
}
var address: Address?
}
class Room {
let name: String
init(name: String) { self.name = name }
}
class Address {
var buildingName: String?
var buildingNumber: String?
var street: String?
func buildingIdentifier() -> String? {
if let buildingNumber = buildingNumber, let street = street {
return "\(buildingNumber) \(street)"
} else if buildingName != nil {
return buildingName
} else {
return nil
}
}
}
- buildingNumber, street λ λ€ μμΌλ©΄ λ°ν μ€ν¨νλ©΄ buildingName λ°ν, μ΄κ²λ μ€ν¨νλ©΄ nil λ°ν
- κ°μ μΈλνμ λμμΌλ‘ μ΅μ λ 체μ΄λμ μ¬μ©νλ©΄, μ΅μ λ κ°μ μμ±μ μ κ·Όνκ³ ν΄λΉ μμ±μ΄ μ κ·Όμ΄ μ±κ³΅μ μΈμ§ νμΈν μ μλ€.
- μ΅μ λ 체μ΄λμ μ΄μ©ν΄ νλ‘νΌν°μ μ κ·Όν μ μλ€.
let john = Person()
if let roomCount = john.residence?.numberOfRooms {
print("John's residence has \(roomCount) room(s).")
} else {
print("Unable to retrieve the number of rooms.")
}
// Prints "Unable to retrieve the number of rooms."
- residence?κ° nilμ΄κΈ° λλ¬Έμ μ΅μ λ 체μ΄λ κ²°κ³Ό nil νΈμΆ
func createAddress() -> Address {
print("Function was called.")
let someAddress = Address()
someAddress.buildingNumber = "29"
someAddress.street = "Acacia Road"
return someAddress
}
john.residence?.address = createAddress()
- μ€ν κ²°κ³Ό "Function was called.βκ° μΆλ ₯λμ§ μμ β λ©μλκ° μμ μ€νλμ§ μμλ€.
- john.residence?κ° nilμ΄κΈ° λλ¬Έμ ν λΉ λΆκ°λ₯
- μ΅μ λ 체μ΄λμ μ¬μ©ν΄ λ©μλλ₯Ό νΈμΆν μ μλ€.
- λ©μλμμ λ¦¬ν΄ κ°μ μ μνμ§ μμλ μμ μν κ°λ₯
func printNumberOfRooms() {
print("The number of rooms is \(numberOfRooms)")
}
- λ¦¬ν΄ κ°μ΄ λͺ
μλμ§ μμμ§λ§ μλμΌλ‘ Void? νμ
μ κ°λλ€.
- μ΅μ λ 체μ΄λμ μ¬μ©νμ¬ μ΅μ λ κ°μ λ°ν
if john.residence?.printNumberOfRooms() != nil {
print("It was possible to print the number of rooms.")
} else {
print("It was not possible to print the number of rooms.")
}
// Prints "It was not possible to print the number of rooms."
- λ€μκ³Ό κ°μ΄ nilλ‘ λΉκ΅ν΄μ μ λλ‘ λ©μλκ° μ€νμ΄ λλμ§ νμΈ κ°λ₯
if (john.residence?.address = someAddress) != nil {
print("It was possible to set the address.")
} else {
print("It was not possible to set the address.")
}
// Prints "It was not possible to set the address."
- νλ‘νΌν°λ₯Ό μΈν νλ κ²λ λ€μκ³Ό κ°μ λ°©μμΌλ‘ νμΈ κ°λ₯
- μ΅μ λ 체μ΄λμ μ΄μ©ν΄ μ΅μ λ κ°μ μλΈμ€ν¬λ¦½νΈλ‘ μ κ·Ό κ°λ₯
- μλΈμ€ν¬λ¦½νΈμ λκ΄νΈ μμ ?κ° λΆλλ€. (νμ μ΅μ λ ννμ λ°λ‘ λ€μ ?κ° λΆλλ€.)
if let firstRoomName = john.residence?[0].name {
print("The first room name is \(firstRoomName).")
} else {
print("Unable to retrieve the first room name.")
}
// Prints "Unable to retrieve the first room name."
- ν λΉλ κ°λ₯
john.residence?[0] = Room(name: "Bathroom")
// residenceκ° nilμ΄λΌ μ€ν¨
μλΈμ€ν¬λ¦½νΈκ° μ΅μ λ κ°μ λ°ννλ κ²½μ°(λμ λ리 νμ ) μλΈ μ€ν¬λ¦½νΈλ₯Ό λ«λ λκ΄νΈ λ€μ ?λ₯Ό μΆκ°νμ¬ μ΅μ λ κ°μ μ°κ²°νλ€.
var testScores = ["Dave": [86, 82, 84], "Bev": [79, 94, 81]]
testScores["Dave"]?[0] = 91
testScores["Bev"]?[0] += 1
testScores["Brian"]?[0] = 72
// the "Dave" array is now [91, 82, 84] and the "Bev" array is now [80, 94, 81]
- λμ λλ¦¬κ° key κ°μ μλͺ» λ£μ΄μ λ°νμ μ€ν¨ν΄λ μ€λ₯κ° λμ§ μκ³ nilμ΄ λ°νλλ€.
- μ΅μ λ 체μ΄λμ΄ μ¬λ¬ λ¨κ³μ κ±Έμ³ μ°κ²°λ μ μλ€.
- μ¬λ¬ λ¨κ³κ° κ²Ήμ³λ λ μ΅μ λν΄μ§μ§λ μλλ€. (μμ κ°κ³Ό νμ κ°μ΄ λ€ μ΅μ λμ΄λΌκ³ μ΅μ λ+κ° λμ§ μλλ€.)
λ€λ₯Έ λ§λ‘ νμλ©΄
- κ²μνλ €λ νμ μ΄ μ΅μ λμ΄ μλ κ²½μ°μλ μ΅μ λ 체μ΄λμΌλ‘ μ΅μ λ νμ μ΄ λλ€.
- κ²μνλ €λ νμ μ΄ μ΄λ―Έ μ΅μ λμΈ κ²½μ° μ΅μ λ 체μ΄λ λλ¬Έμ λ μ¬ν μ΅μ λμ΄ λμ§ μλλ€.
λ°λΌμ
- μ΅μ λ 체μ΄λμ ν΅ν΄ Int κ°μ κ²μνλ €κ³ νλ©΄ μ²΄μΈ μμ€μ κ΄κ³ μμ΄ νμ Int?κ° λ°νλλ€.
- λ§μ°¬κ°μ§λ‘ μ΅μ λ 체μ΄λμ ν΅ν΄ Int? κ°μ κ²μνλ €κ³ νλ©΄ μ¬λ¬ λ¨κ³μ μκ΄μμ΄ Int?κ° λ°νλλ€.
let johnsHouse = Residence()
johnsHouse.rooms.append(Room(name: "Living Room"))
johnsHouse.rooms.append(Room(name: "Kitchen"))
john.residence = johnsHouse
if let johnsStreet = john.residence?.address?.street {
print("John's street name is \(johnsStreet).")
} else {
print("Unable to retrieve the address.")
}
// Prints "Unable to retrieve the address."
- 2λ¨κ³μ μ΅μ λ 체μ΄λμΌλ‘ μ°κ²°λμ΄ μλ€.
- residence?λ μ ν¨νμ§λ§ address?μ κ°μ΄ nilμ΄λ―λ‘ μ΅μ λ 체μ΄λμ μ€ν¨νλ€.
- μ΅μ λ 체μ΄λμ΄ μ±κ³΅ν κ²½μ°, streetλ String? νμ μ κ°μ λ°ννλ€.
- μ΅μ λ 체μ΄λμμ λ°ν κ°μ΄ μλ λ©μλλ₯Ό νΈμΆν μ μλ€.
if let buildingIdentifier = john.residence?.address?.buildingIdentifier() {
print("John's building identifier is \(buildingIdentifier).")
}
// Prints "John's building identifier is The Larches."
- buildingIdentifier()μ λ°ν κ°μ String?μ΄λ€
if let beginsWithThe =
john.residence?.address?.buildingIdentifier()?.hasPrefix("The") {
if beginsWithThe {
print("John's building identifier begins with \"The\".")
} else {
print("John's building identifier doesn't begin with \"The\".")
}
}
// Prints "John's building identifier begins with "The"."
- λ€μκ³Ό κ°μ΄ λ©μλμ λ°ν κ°μ μ΅μ λ 체μ΄λ ν μ μλ€.