μλ νμΈμ, κ°μμ λλ€. μ€λλ§μ λμμ¨ κ² κ°μλ°μ, (μλκ° γ ) μμ¦ μ€ν°λλ§ μ§ννκ³ μ€ν°λν λ΄μ©μ μ λ‘λνλ κ²μ κΉλ¨Ήμλ€μ. κ·Έλ¦¬κ³ μ¬λμ΄λΌλκ²,, μ‘°κΈ μμ¦ λνν΄μ Έμ γ γ λ€μ νλ² νμ΄ν μΈμΉκ³ κ°μμ΄κΈ° λ€μ μμν©λλ€ νν
Swift Document chap16. optional chaining 보λ¬κ°κΈ°
Optional Chanining (μ΅μ λ 체μ΄λ)
μ΅μ λ 체μ΄λμ nilμΌ μ λ μλ νλ‘νΌν°λ, λ©μλ κ·Έλ¦¬κ³ μλΈμ€ν¬λ¦½νΈμ Query(μ§λ¬Έ)λ₯Ό νλ κ³Όμ μ λ§νλ€.
λ§μ½ μ΅μ λμ΄ νλ‘νΌν°λ λ©μλ νΉμ μλΈμ€ν¬λ¦½νΈμ λν κ°μ κ°κ³ μλ€λ©΄ κ·Έ κ°μ λ°ννκ³ λ§μ½ κ°μ΄ nil μ΄λΌλ©΄ nilμ λ°ννλ€. μ¬λ¬ κ°μ 쿼리λ₯Ό νμΈνλ©΄ μ μ μλλ° μ°κ²°λ 체μΈμ€ νλλΌλ κ²°κ³Όκ°μ΄ nilμ΄λΌλ©΄ μ 체 체μΈμ κ²°κ³Όλ nilμ΄ λ°νλλ€.
κ°μ μΈλνμ λ체λ‘μ¨μ μ΅μ λ 체μ΄λ
μ΅μ λ 체μ΄λμ μ΅μ λ κ° λ€μ λ¬Όμν(?)λ₯Ό λΆμ¬μ νν κ°λ₯νλ€. μ΅μ λμ μ¬μ©ν μ μλ κ°μλ νλ‘νΌν°, λ©μλ, κ·Έλ¦¬κ³ μλΈμ€ν¬λ¦½νΈκ° ν¬ν¨λλ€. μ΅μ λ κ°μ κ°μ μΈλν νκΈ° μν΄μ λ€μ λλν(!)λ₯Ό λΆμ΄λ κ²κ³Ό λ¬Έλ²μ΄ λΉμ·νλ°, κ°μ₯ ν° μ°¨μ΄λ κ°μ μΈλνμ νλλ° λ§μ½ κ·Έ κ°μ΄ μμΌλ©΄ λ°νμ μλ¬κ° λ°μνμ§λ§, μ΅μ λ 체μ΄λμ μ¬μ©νλ©΄ λ°νμ μλ¬ λμ nil μ΄ λ°νλλ€λ κ²μ΄λ€.
μ΅μ λ 체μ΄λμ μν΄ nil κ°μ΄ νΈμΆ λ μ μκΈ° λλ¬Έμ μ΅μ λ 체μ΄λμ λ°νκ°μ νμ μ΅μ λ κ°μ΄ λλ€. (Intλ₯Ό λ°ννλ λ©μλμΈ κ²½μ°, μ΅μ λ 체μ΄λμ΄ μ±κ³΅μ μΌλ‘ μ€νλλ©΄ Int?κ° λ°ν)
λ€μ μ½λλ μ΅μ λ 체μ΄λμ΄ κ°μ μΈλνκ³Ό μ΄λ»κ² λ€λ₯Έμ§ 보μ¬μ£Όλ μμ μ΄λ€.
class Person {
var residence: Residence?
}
class Residence {
var numberOfRooms = 1
}
Residence μΈμ€ν΄μ€λ numberOfRoomsμ΄λΌλ Intνλ‘νΌν°λ₯Ό νλ μμ νκ³ μκ³ , Person μΈμ€ν΄μ€λ μ΅μ λ residence νλ‘νΌν°λ₯Ό Residence? λ‘ μμ νλ€. λ§μ½ Person μ΄λΌλ μΈμ€ν΄μ€λ₯Ό μμ±νλ©΄ residence νλ‘νΌν°λ μ΅μ λμ κΈ°λ³Έ κ°μΈ nil λ‘ μ΄κΈ°ν λλ€.
let john = Person()
μ¦, μ΄ μ½λμμ johnμ residence νλ‘νΌν°κ° nil μΈ κ°μ μμ νκ² λλ€.
let roomCount = john.residence!.numberOfRooms
//ERROR!!!!
λ§μ½ μ΄ Personμ residenceμ numberOfRooms νλ‘νΌν°μ μ κ·ΌνκΈ° μν΄ λλνλ₯Ό μ΄μ©ν΄ κ°μ μΈλνμ μλνλ€λ©΄, residence κ° μΈλνν κ°μ΄ μκΈ° λλ¬Έμ λ°νμ μλ¬κ° λ°μνλ€.
numberOfRooms μ κ°μ μ κ·ΌνκΈ° μν΄ κ°μ μΈλν λμ μ΅μ λ 체μ΄λμ μ¬μ©ν μ μλ€. μ΅μ λ 체μ΄λμ μ¬μ©νκΈ° μν΄ λλν! λμ λ¬Όμν?λ₯Ό μ¬μ©νλ€.
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 νλ‘νΌν°λ€μ μλ‘ λ¬Άμ¬μλ€. κ·Έλμ μ μ½λμμ numberOfRoomsνλ‘νΌν°λ μ΅μ λμ΄ μλμ§λ§ μ΄ κ°μ μ κ·ΌνκΈ° μν΄ μ¬μ©νλ μΈμ€ν΄μ€μ νλ‘νΌν° residence κ° μ΅μ λμ΄κΈ° λλ¬Έμ μ΅μ’ κ°μ μ΅μ λμ΄ λλ€.
john.residence = Residence()
if let roomCount = john.residence?.numberOfRooms {
print("John's residence has \\(roomCount) room(s).")
} else {
print("Unable to retrieve the number of rooms.")
}
// Prints "John's residence has 1 room(s)."
nil μ΄μλ residenceκ°μ ResidenceμΈμ€ν΄μ€λ₯Ό μμ±ν΄μ μΆκ°ν μ μλ€.
john.residence λ μ΄μ nil μ΄ μλλΌ Residence() μΈμ€ν΄μ€λ₯Ό κ°λλ€. κ·Έλμ μ΅μ λ 체μ΄λμΌλ‘ numberOfRoomsκ°μ μ κ·Ό νμ λ 1μ 리ν΄νλ€.
μ΅μ λ 체μ΄λμ μν λͺ¨λΈ ν΄λμ€ μ μ
μ΅μ λ 체μ΄λμ νλ‘νΌν°, λ©μλ, κ·Έλ¦¬κ³ μλΈμ€ν¬λ¦½νΈμμ μ¬μ©ν μ μλλ°, ν λ¨κ³κ° μλ μ¬λ¬ λ¨κ³λ‘ μ¬μ©ν μ μλ€. μ‘°κΈ λ 볡μ‘ν μ½λ μμ λ‘ μ΄ν΄λ³΄μ.
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
}
}
}
μ μ½λλ Person, Residence λͺ¨λΈμ νμ₯νμ¬ Roomκ³Ό Address ν΄λμ€λ₯Ό μΆκ°ν λͺ¨λΈμ μ μνλ€.
- Person ν΄λμ€λ μμ λμΌ
- Residence
- roomsλΌλ νλ‘νΌν°λ₯Ό μμ νλ€. [Room] νμ μ λΉ λ°°μ΄λ‘ μ΄κΈ°ν λλ€. μ΄μ λ²μ μ Residenceκ° Room μΈμ€ν΄μ€μ λ°°μ΄μ μμ νκ³ μκΈ° λλ¬Έμ numberOfRooms νλ‘νΌν°λ κ³μ° νλ‘νΌν°λ‘ μ μΈλλ€. rooms λ°°μ΄μ μ κ·ΌνκΈ° μν λ¨μΆμ΄λ‘ μλΈμ€ν¬λ¦½νΈλ₯Ό μ μΈνμ.
- Room
- rooms λ°°μ΄μ Roomν΄λμ€λ, μ΄λ¦ νλλ₯Ό μ΄κΈ°ν λ μΈμλ‘ λ°λ κ°λ¨ν ν΄λμ€
- Address
- μ΄ ν΄λμ€λ 3κ°μ String? μ΅μ λ νλ‘νΌν°λ₯Ό κ°λλ€. μ²μ λκ°λ buildingName, buildingNumber μ΄κ³ λΉλ© μ£Όμλ₯Ό ꡬλΆνλ€. λ§μ§λ§ street νλ‘νΌν°λ 거리μ μ£Όμλ₯Ό λνλΈλ€.
- buildingIdentifier() λΌλ String? νμ μ λ©μλλ₯Ό μ§μνλ€. μ΄ λ©μλλ buildingNumberμ streetμ νμΈν΄ κ°μ΄ μμΌλ©΄ buildingNumberμ κ²°ν©λ street κ°μ λ³ννκ³ μμΌλ©΄ 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."
let someAddress = Address()
someAddress.buildingNumber = "29"
someAddress.street = "Acacia Road"
john.residence?.address = someAddress
μ μ½λμμ, residence?κ° nil μ΄κΈ° λλ¬Έμ μ΅μ λ 체μ΄λ κ²°κ³Ό nilμ νΈμΆνκ² λλ€.
μ΅μ λ 체μ΄λμ κ°μ ν λΉν λλ μ¬μ© κ°λ₯νλ€. Address() μΈμ€ν΄μ€λ₯Ό μμ±ν΄ john.residenceμ addressλ‘ ν λΉνλ μ½λμ΄λ€. john.residence?κ° nilμ΄κΈ° λλ¬Έμ address ν λΉμ μ€ν¨νκ² λλ€. ν λΉλ ν λΉλ°λ μΌμͺ½ νμ΄ nilμ΄λ©΄ μμ μ€λ₯Έμͺ½ νμ΄ μ€νλμ§ μλλ€.
func createAddress() -> Address {
print("Function was called.")
let someAddress = Address()
someAddress.buildingNumber = "29"
someAddress.street = "Acacia Road"
return someAddress
}
john.residence?.address = createAddress() //λ°μ μμ
μ§μ§λ‘ μΌμͺ½νμ΄ nilμ΄λ©΄ μ€λ₯Έμͺ½νμ΄ μμ μ€νλμ§ μλμ§ μμ보기 μν΄ createAddressλ©μλλ₯Ό λ§λ€μλ€. john.residence?.address = createAddress() μμ νΈμΆνλ©΄ “Function was called” κ° μΆλ ₯λμ§ μλλ€. μ΄ λ©μλκ° μ€νλμ§ μμμμ μλ―Ένλ€.
μ΅μ λ 체μ΄λμ ν΅ν λ©μλ νΈμΆ
μ΅μ λ 체μ΄λμ μ΄μ©νμ¬ λ©μλλ₯Ό νΈμΆ ν μ μλ€.
func printNumberOfRooms() {
print("The number of rooms is \\(numberOfRooms)")
}
μ λ©μλλ 리ν΄κ°μ΄ λͺ μλμ§ μλλ€. νμ§λ§ ν¨μλ λ©μλλ 리ν΄κ°μ΄ μλ κ²½μ° μμμ μΌλ‘ VoidλΌλ κ°μ κ°λλ€. κ·Έλμ μ΄ λ©μλκ° μ΅μ λ 체μ΄λμμ νΈμΆλλ©΄ λ°νκ°μ Voidκ° μλλΌ 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."
μ μ½λλ john.residence?.printNumberOfRooms() λ©μλ νΈμΆμ κ²°κ³Όκ° nilμΈμ§ μλμ§ λΉκ΅λ₯Ό νκ³ κ·Έ κ²°κ³Όμ λν μ²λ¦¬λ₯Ό νλ€. μμμ μΈκΈνλ κ²μ²λΌ printNumberofRooms()λ Void?κ° λ°νλμ nil λΉκ΅λ₯Ό ν μ μλ€.
μ΅μ λ 체μ΄λμ ν΅ν΄ κ°μ ν λΉνλ©΄ κ°μ Void?λ‘ λ°ννλ€. κ·Έλμ μ΄ κ°μ΄ 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."
μ μ½λλ μλΈμ€ν¬λ¦½νΈλ₯Ό μ΄μ©ν΄ rooms μμ 첫 rooms μ nameμ μμ²νλ μ½λμ΄λ€. νμ¬ john.residence κ° nil μ΄κΈ° λλ¬Έμ μλΈμ€ν¬λ¦½νΈ μ κ·Όμ μ€ν¨νκ² λλ€.
- μΆκ°μ€λͺ
- μ΅μ λ 체μ΄λμμ μλΈμ€ν¬λ¦½νΈλ‘ κ°μ κ°μ Έμ€λ κ²κ³Ό μ μ¬ν ννλ‘ κ°μ ν λΉν μ μλ€. john.residence?[0] = Room (name: “Bathroom”) μ΄ μ½λλ 첫 room κ°μ ν λΉνλ μ½λμΈλ° roomsμ ν λΉνμ§ λͺ»νκ³ failμ΄ λ°μνλ€. κ·Έ μ΄μ λ residence κ° νμ¬ nil μ΄κΈ° λλ¬Έμ΄λ€.
let johnsHouse = Residence()
johnsHouse.rooms.append(Room(name: "Living Room"))
johnsHouse.rooms.append(Room(name: "Kitchen"))
john.residence = johnsHouse
if let firstRoomName = john.residence?[0].name {
print("The first room name is \\(firstRoomName).")
} else {
print("Unable to retrieve the first room name.")
}
// Prints "The first room name is Living Room."
μ μ½λμ κ°μ΄ Residence μΈμ€ν΄μ€λ₯Ό ν λΉνλ©΄ residence μλΈμ€ν¬λ¦½νΈλ₯Ό μ¬μ©ν μ μλ€.
μ΅μ λ νμ μ μλΈμ€ν¬λ¦½νΈ μ κ·Ό
λ§μ½ swiftμ Dictionary νμ μ΄ μλΈμ€ν¬λ¦½νΈμ κ²°κ³Όλ‘ μ΅μ λ κ°μ λ°ννλ€κ³ νλ©΄, κ·Έ λ€μ λ¬Όμν?λ₯Ό λΆμ¬ μ€λ€. Dictionary νμ μ key-value λ‘ λμνκΈ° λλ¬Έμ νμ κ·Έ μ¬μ μ keyκ° μ‘΄μ¬νλ€λ 보μ₯μ΄ μκΈ° λλ¬Έμ μ΅μ λ κ°μ λ°ννλ€.
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]
μ μ½λλ testScores λΌλ Dictionaryμμ νΉμ keyμ λν κ°μ get νΉμ set νλ μ½λμ΄λ€. Dave, Bevμ κ΄λ ¨ν κ°μ μ²λ¦¬κ° λκ³ , Brainμ λ±λ‘λ key κ° μκΈ° λλ¬Έμ κ΄λ ¨ν μ무μΌλ μΌμ΄λμ§ μλλ€.
체μ΄λμ λ€μ€ μ°κ²°
μ΅μ λ 체μ΄λμ΄ μ¬λ¬ λ¨κ³μ κ±Έμ³ μ°κ²° λ μ μλ€. μμ κ°μ΄ μ΅μ λ κ°μΈ κ²½μ° νμλ μ΅μ λ κ°μ΄λΌκ³ ν΄μ λ μ΅μ λ ν΄μ§μ§ μλλ€. μ΅μ λμ μ΅μ λμ΄λ€.
- μ΅μ λ 체μ΄λμ μμ λ 벨 κ°μ΄ μ΅μ λμΈ κ²½μ° νμ¬ κ°μ΄ μ΅μ λμ΄ μλλλΌλ κ·Έ κ°μ μ΅μ λκ°μ΄ λλ€.
- μ΅μ λ 체μ΄λμ μμ λ 벨 κ°μ΄ μ΅μ λμ΄κ³ νμ¬ κ°μ΄ μ΅μ λ μ΄λΌκ³ ν΄μ λ μ΅μ λνκ² λμ§ μλλ€.
- μ΅μ λ 체μ΄λμ ν΅ν΄ κ°μ κ²μνκ±°λ λ©μλλ₯Ό νΈμΆνλ©΄ λͺ λ¨κ³λ₯Ό κ±°μΉλμ§ μκ΄μμ΄ μ΅μ λμ λ°ννλ€.
μμ λ₯Ό μ΄ν΄λ³΄μ.
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."
john.residenceλ validν Residence μΈμ€ν΄μ€λ₯Ό κ°κ³ μμ§λ§ addressκ° nilμ΄μ΄μ μ΄ μ΅μ λ 체μ΄λμ failμ΄ λλ€. λΉλ‘ μμμ μ΅μ λ κ°μΈ john.residenceκ° trueμ¬λ νμ κ°μ΄ nil μ΄λ©΄ 체μ΄λμ failμ΄ λλ€.
let johnsAddress = Address()
johnsAddress.buildingName = "The Larches"
johnsAddress.street = "Laurel Street"
john.residence?.address = johnsAddress
if let johnsStreet = john.residence?.address?.street {
print("John's street name is \\(johnsStreet).")
} else {
print("Unable to retrieve the address.")
}
// Prints "John's street name is Laurel Street."
μ μ½λλ Address μΈμ€ν΄μ€λ₯Ό μμ±μμ ν λΉνκ³ λ€μ μ΅μ λ 체μ΄λμ ν΄λ³Έ μ½λμ΄λ€. Residence μΈμ€ν΄μ€μ Address μΈμ€ν΄μ€κ° λͺ¨λ μ‘΄μ¬νκΈ° λλ¬Έμ μ΅μ λ 체μ΄λμ sucess κ° λλ€.
체μ΄λμμ μ΅μ λ κ°μ λ°ννλ λ©μλ
μλ μ½λλ μ΅μ λ 체μ΄λμμ λ°ν κ°μ΄ μλ λ©μλλ₯Ό νΈμΆ νλ μ½λμ΄λ€.
if let buildingIdentifier = john.residence?.address?.buildingIdentifier() {
print("John's building identifier is \\(buildingIdentifier).")
}
// Prints "John's building identifier is The Larches."
λ§μ½ λ©μλμ λ°ν κ°μ κ°κ³ μΆκ°μ μΈ νλμ λ νκ³ μΆλ€λ©΄ λ©μλ νΈμΆ νν λ€μ λ¬Όμν(?)λ₯Ό λΆμ΄κ³ κ·Έ νλμ μ μ΄μ£Όλ©΄ λλ€. μμμ μΈκΈ νλ κ² μ²λΌ μ΅μ λ 체μ΄λμ λ¬Όλ € μκΈ° λλ¬Έμ λ©μλμ λ°ν κ°λ μ΅μ λμ΄ λΌμ κ·Έ νμλ₯Ό ν΄μ€μΌ νλ€.
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 does not begin with \\"The\\".")
}
}
// Prints "John's building identifier begins with "The"."
μ΅μ λμ΄ λ°ν κ°μ κ±Έλ €μκ³ , λ©μλ μ체μ κ±Έλ €μλ κ²μ΄ μλκΈ° λλ¬Έμ λ©μλ κ΄νΈ λ€μ ? λ₯Ό λΆμ¬μ€μΌ νλ€.