[Swift] 딕셔너리 / Dictionary

2022. 9. 1. 02:24🍏/Swift

Dictionary

Generic Structure
A collection whose elements are key-value pairs.

@frozen struct Dictionary<Key, Value> where Key : Hashable
ar responseMessages = [200: "OK",
                        403: "Access forbidden",
                        404: "File not found",
                        500: "Internal server error"]


var emptyDict: [String: String] = [:]

 

print(responseMessages[200])
// Prints "Optional("OK")"

let httpResponseCodes = [200, 403, 301]
for code in httpResponseCodes {
    if let message = responseMessages[code] {
        print("Response \(code): \(message)")
    } else {
        print("Unknown response \(code)")
    }
}
// Prints "Response 200: OK"
// Prints "Response 403: Access forbidden"
// Prints "Unknown response 301"

'🍏 > Swift' 카테고리의 다른 글

[Swift] 오버라이딩 / Overriding  (0) 2022.09.02
[Swift] 캡쳐 / Capture  (0) 2022.09.01
[Swift] 세트 / Set  (0) 2022.09.01
[Swift] 배열 / Array  (0) 2022.09.01
[Swift] @autoclosure / 지연된 실행  (0) 2022.08.31