nil
์ผ ๊ฐ๋ฅ์ฑ์ด ์๋ ํ๋กํผํฐ, ๋ฉ์๋, ์๋ธ์คํฌ๋ฆฝํธ๋ฅผ ์กฐํํ๊ณ ํธ์ถํ๊ธฐ ์ํ ํ๋ก์ธ์ค.nil
์ด๋ฉด nil ์ ๋ฐํํ๋ค.nil
์ด ์๋ ๊ฒฝ์ฐ opt?
์ฒ๋ผ ? ๋ฅผ ์ฌ์ฉํด์ ์ต์
๋ ์ฒด์ด๋์ ์ฌ์ฉํ ์ ์์.opt!
์ ๋ค๋ฅธ ์ ์ opt ๊ฐ์ด nil ์ด์ด๋ ๋ฐํ์์๋ฌ๊ฐ ๋์ง ์๋๋ค๋ ์ ์ด๋ค.class Person {
var residence: Residence?
}
class Residence {
var numberOfRooms = 1
}
let john = Person()
let roomCount = john.residence!.numberOfRooms // error
// ์๋์ roomCount ๋ Int? ํ์
์ด ๋จ.
// john.residence?.numberOfRooms ๊ฐ Optional Chaining
// ๐ if let ๊ตฌ๋ฌธ(Optional Binding) ๊ณผ ๊ฒฐํฉ!
if let roomCount = john.residence?.numberOfRooms {
// roomCount ๊ฐ nil์ด ์๋ ๋ ์คํ.
} else {
// roomCount ๊ฐ nil์ผ ๋ ์คํ.
}
john.residence = Residence() // residence != nil
class Person {
var residence: Residence?
}
class Residence {
// ํ๋กํผํฐ์ ์๋ธ์คํฌ๋ฆฝํธ, ๋ฉ์๋ ์ ์.
// rooms ๋ฐฐ์ด.
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()
// ์๋์ฒ๋ผ nil์ผ ์ ์๋ class ์์ ์์์ chaining ์ผ๋ก ์ ๊ทผ ๊ฐ๋ฅ.
if let roomCount = john.residence?.numberOfRooms {
print("John's residence has \(roomCount) room(s).")
} else {
print("Unable to retrieve the number of rooms.")
}
// optional chaining ์ผ๋ก ์ป์ด์จ ๊ฒ์ ๊ฐ์ ํ ๋นํ ์ ์๋ค.
let someAddress = Address()
someAddress.buildingNumber = "29"
someAddress.street = "Acacia Road"
john.residence?.address = someAddress
// ํ์ง๋ง chaining ์์ 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)")
}
// residence == nil
if john.residence?.printNumberOfRooms() != nil { // nil
print("It was possible to print the number of rooms.")
} else {
print("It was not possible to print the number of rooms.")
}
// ํ๋กํผํฐ ๊ฐ์ ์ค์ ํ๋ ํํ์๋ ํ๊ฐํ ์ ์๋ค.
// ํ๋กํผํฐ ๊ฐ์ด ์ค์ ์ด ๋จ -> Void
// ์๋จ -> nil ์ด๋ผ ํ์ธ ๊ฐ๋ฅ.
if (john.residence?.address = someAddress) != nil { // nil
print("It was possible to set the address.")
} else {
print("It was not possible to set the address.")
}
arr?[0]
์ฒ๋ผ ๋๊ดํธ ์ ์ Optional Chaining ์ ํ๋ฉด ๋๋ค.// ์๋ ๋ฌธ์ฅ๊ณผ ๊ฐ์ ๋ป. subscript ๋ฅผ ์ ์ํ๊ธฐ ๋๋ฌธ์ ์๋์ฒ๋ผ ์ธ ์ ์๋ ๊ฒ!!
// if let firstRoomName = john.residence?.rooms[0].name {
if let firstRoomName = john.residence?[0].name {
print("The first room name is \(firstRoomName).")
} else {
print("Unable to retrieve the first room name.")
}
john.residence?[0] = Room(name: "Bathroom")
dict[key][0]
์ด๋ฐ์์ผ๋ก ์ ๊ทผํ ์ ์๋ค.dict[key]
๋ Optional ์ด๋ฏ๋ก ์ฌ๊ธฐ์๋ Chaining ์ ์ฉ ๊ฐ๋ฅ.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]
john.residence?.address?.buildingIdentifier()?.hasPrefix("The")
+) 0401 ์ถ๊ฐ ์ ๋ฆฌ
<๋ฒ๋ค์ ์ด์ผ๊ธฐ>
์ถ์ฒ
https://bbiguduk.gitbook.io/swift/language-guide-1/optional-chaining