Bibi's DevLog π€π
Dictionary - subscript(_: default:) λ³Έλ¬Έ
π±π iOS/π Apple Developer Documentation
Dictionary - subscript(_: default:)
λΉλΉ bibi 2022. 8. 25. 23:59subscript(_: default:)
(Dictionary)
https://developer.apple.com/documentation/swift/dictionary/subscript(_:default:)
- λμ
λ리μμ μ£Όμ΄μ§ ν€μ ν΄λΉνλ κ°μ 리ν΄νκ³ , λ§μ½ ν΄λΉνλ ν€κ° μμΌλ©΄
default
λ‘ μ§μ λ κ°μ λ°ννλ€. - key : λμ
λ리μμ μ°Ύμ ν€
- keyκ°
Hashable
μ μ€μν΄μΌ μ¬μ© κ°λ₯ν¨
- keyκ°
- defaultValue : λμ λ리μ ν΄λΉ ν€κ° μ‘΄μ¬νμ§ μμ λ μ¬μ©ν κΈ°λ³Έκ°
- λμ
λ리μ νΉμ ν€κ° μ‘΄μ¬ν λλ κ·Έ κ°μ 리ν΄νκ³ , μ‘΄μ¬νμ§ μμ λλ default valueλ₯Ό 리ν΄νλ μλΈμ€ν¬λ¦½νΈ.
- μλ μμλ HTTP μλ΅ μ½λκ° μΈμλμ§ μλ κ²½μ° μ¬μ©ν λ©μμ§λ‘ μλΈμ€ν¬λ¦½νΈλ₯Ό μ¬μ©νλ€:
var responseMessages = [200: "OK",
403: "Access forbidden",
404: "File not found",
500: "Internal server error"]
let httpResponseCodes = [200, 403, 301]
for code in httpResponseCodes {
let message = responseMessages[code, default: "Unknown response"]
print("Response \(code): \(message)")
}
// Prints "Response 200: OK"
// Prints "Response 403: Access forbidden"
// Prints "Response 301: Unknown response"
- λμ λ리μ value νμ μ΄ value semanticsμ κ°μ§ λ, μ΄ μλΈμ€ν¬λ¦½νΈλ₯Ό μ¬μ©ν΄ λμ λ리 λ΄μμ κ°μ λν΄ μ μ리(in-place) μ°μ°μ ν μ μλ€.
let message = "Hello, Elle!"
var letterCounts: [Character: Int] = [:]
for letter in message {
letterCounts[letter, default: 0] += 1
}
// letterCounts == ["H": 1, "e": 2, "l": 4, "o": 1, ...]
- letterCountsμ ν€κ° μ‘΄μ¬νμ§ μλ μνμμ letterμ κ°μ λν΄
letterCounts[letter, default: 0] += 1
μ°μ°μ΄ μ€νλ λ, - μ§μ λμ΄ μλ defalut valueμΈ 0μ΄ μλΈμ€ν¬λ¦½νΈλ‘λΆν° λ°νλκ³ , 1μ΄ μ¦κ°λ λ€, κ·Έ ν€μ λν κ°μΌλ‘ λμ λ리μ μΆκ°λλ€.
λ ΈνΈ
λμ λ리μ value νμ μ΄ ν΄λμ€μΌ λλ μ΄ μλΈμ€ν¬λ¦½νΈλ‘ λμ λ리 κ°μ μμ νκΈ° μν΄ μ¬μ©νμ§ λ§μμμ€. κ·Έλ¬ν κ²½μ°μλ, ν΄λΉ μ°μ° μ΄νμ default valueμ keyκ° λμ λλ¦¬λ‘ λ€μ μ¨μ§μ§ μμ΅λλ€(not written back).
'π±π iOS > π Apple Developer Documentation' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[π / UIKit] UINavigationController (1) | 2022.09.23 |
---|---|
About the App Launch Sequence μ± μ€ν μνμ€μ λνμ¬ (0) | 2022.08.27 |
[Swift] .zip(_:_:) : λ μνμ€ ν©μΉκΈ° (0) | 2022.06.21 |
[Foundation] UserDefaults (1) | 2022.06.17 |
UINavigationBarμ μμ λ£κΈ° (Customizing the appearance of UINavigationBar) (0) | 2022.05.25 |