λ°μν
μ΅μ λ 체μ΄λ
nilμΌ μλ μλ νλ‘νΌν°λ, λ©μλ κ·Έλ¦¬κ³ μλΈμ€ν¬λ¦½νΈμ μ§μ(query)λ₯Ό νλ κ³Όμ
- λ§μ½ μ΅μ λμ΄ νλ‘νΌν°λ λ©μλ νΉμ μλΈμ€ν¬λ¦½νΈμ λν κ°μ κ°κ³ μλ€λ©΄ κ·Έ κ°μ λ°νν¨
- λ§μ½ κ°μ΄ nilμ΄λΌλ©΄ nil λ°ν
κ°μ μΈλνμ λ체λ‘μ¨μ μ΅μ λ 체μ΄λ
- μ΅μ λ κ° λ€μ λ¬Όμν(?)λ₯Ό λΆμ¬μ νν κ°λ₯
- μ΅μ λμ μ¬μ©ν μ μλ κ°μλ νλ‘νΌν°, λ©μλ κ·Έλ¦¬κ³ μλΉμ€ν¬λ¦½νΈκ° ν¬ν¨
- μ΅μ
λ κ°μ κ°μ μΈλννκΈ° μν΄ λ€μ λλν(!)λ₯Ό λΆμ΄λ κ²κ³Ό μ μ¬
- κ°μ μΈλνμ νλλ° λ§μ½ κ·Έ κ°μ΄ μμΌλ©΄ λ°νμ μλ¬ λ°μ
- μ΅μ λ 체μ΄λμ μ¬μ©νλ©΄ λ°νμ μλ¬ λμ nilμ΄ λ°ν
class Person {
var residence: Residence?
}
class Residence {
var numberOfRooms = 1
}
let roomCount = john.residence!.numberOfRooms
// this triggers a runtime error
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."
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)."
μ΅μ λ 체μ΄λμ μν λͺ¨λΈ ν΄λμ€ μ μ
- ν λ¨κ³κ° μλ μ¬λ¬ λ¨κ³(multi-level optional chaining) μ¬μ©ν μ μμ
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
}
}
}
μ΅μ λ 체μ΄λμ ν΅ν νλ‘νΌν°μ μ κ·Ό
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
- 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()
μ΅μ λ 체μ΄λμ ν΅ν λ©μλ νΈμΆ
func printNumberOfRooms() {
print("The number of rooms is \\(numberOfRooms)")
}
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."
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."
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."
- μ΅μ λ νμ μ μλΈμ€ν¬λ¦½νΈ μ κ·Ό
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]
체μ΄λμ λ€μ€ λ 벨 μ°κ²°
- μ΅μ λ 체μ΄λμ μμ λ 벨 κ°μ΄ μ΅μ λμΈ κ²½μ° νμ¬ κ°μ΄ μ΅μ λμ΄ μλλλΌλ κ·Έ κ°μ μ΅μ λκ°μ΄ λ©λλ€.
- μ΅μ λ 체μ΄λμ μμ λ 벨 κ°μ΄ μ΅μ λμ΄κ³ νμ¬ κ°μ΄ μ΅μ λ μ΄λΌκ³ ν΄μ λ μ΅μ λνκ² λμ§ μμ΅λλ€.
- μ΅μ λ 체μ΄λμ ν΅ν΄ κ°μ κ²μνκ±°λ λ©μλλ₯Ό νΈμΆνλ©΄ λͺ λ¨κ³λ₯Ό κ±°μΉλμ§ μκ΄μμ΄ μ΅μ λμ λ°νν©λλ€.
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."
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."
체μ΄λμμ μ΅μ λ κ°μ λ°ννλ λ©μλ
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"."
λ°μν
'β¨οΈ Language > swift' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[Swift] νλ‘ν μ½ (0) | 2024.03.29 |
---|---|
[Swift] μλ¬ μ²λ¦¬ (0) | 2024.03.28 |
[Swift] μ΄κΈ°ν (1) | 2024.03.25 |
[Swift] μμ (0) | 2024.03.22 |
[Swift] μλΈμ€ν¬λ¦½νΈ (0) | 2024.03.22 |