A small extension for Foundation, making development easier

During the development process, the programmer quiet often comes across a dictionary like [String: Any] (server response as JSON) or [AnyHashable: Any] (userInfo upon push notification). At the same time, sometimes it is necessary to create a dictionary of this type, while it is undesirable to specify the key as String literal. A small extension for Dictionary presented below solves this problem and somewhat simplifies the development process.

Declaring keys in Enum

As a result, you can get the value of the dictionary both using a string and using a case of enumeration:

let dict: [String: Any] = ["data": 1, "id": 2, "type": 3]
let result = dict["type"]
let sameResult = dict[.type]

Type inference

let dict: [String: Any] = ["data": 1, "id": 2, "type": 3]
let result = dict["id"] as? Int

Much more convenient to move the downcast to extension, the benefit of Swift language has ample opportunities for implementation. Must be declared subscript with generic type:

If there is such subscript, then there is no need to cast the value to the desired type. Accordingly, uses all the possibilities of Swift for type inference:

let dict: [String: Any] = ["data": 1, "id": 2, "type": 3]
someMethod(optionalIntegerArgument: dict[.id])XCTAssertEqual(dict[.type], 3)let result: Int? = dict["type"]
let sameResult: Int? = dict[.type]

Creating a dictionary

In fact, this extension allows you to preform mapping to any rawValue implementing Hashable protocol, not only to strings. Therefore the extension is slightly more versatile. Nethertheless, the task was solved a little more gracefully.

enum Payment: String {
case cash, card, credit

let dict: [Payment: Double] = [.cash: 10, .card: 20, .credit: 0]
let result = dict.mappedToRawValues
XCTAssertEqual(result, ["cash": 10.0, "card": 20.0, "credit": 0.0])