EasyDictionary 0.2.4

EasyDictionary 0.2.4

Maintained by Niels Koole, Paul van Roosendaal, Roadmap.



EasyDictionary

CI Status Version License Platform Twitter

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

  • Swift 4.0

Installation

EasyDictionary is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod 'EasyDictionary'

Summary

This little helper provides some methods to get optional and non-optional values from your dictionary. This can be used anywhere, but we use it mostly to parse data from an endpoint into an object.

Features

  • Generic way of getting types from a dictionary.
  • Parse urls directly.
  • Parse dates directly.
  • Parse timezones from a date string. (of course since we build THE travel app ;-)
  • Any more requests?

Usage

Retrieve the data

import EasyDictionary

let dictionary: [String: Any] = [
    "anInteger": 42,
    "originDate": "2018-05-03T07:15:00+02:00",
    "notJustAnUrl": "https://www.getroadmap.com/"
]

/// Get the integer from the dictionary.
let answerToEverything: Int = try dictionary.required("anInteger") // 42
let answerToEverything: Int? = dictionary.optional("anInteger") // Optional(42)
let answerToEverything: Int = try dictionary.required("doesntExists") // throws error!!

/// Get the url from the dictionary.
let bestTravelApp: URL = try dictionary.requiredUrl("notJustAnUrl") // https://www.getroadmap.com/
let bestTravelApp: URL? = dictionary.optionalUrl("notJustAnUrl") // Optional(https://www.getroadmap.com/)
let bestTravelApp: URL = try dictionary.requiredUrl("doesntExists") // throws error!!

/// Get the date from the dictionary
let dateFormatter: DateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssz"

let originsDate: Date = try dictionary.requiredDate("originDate", dateFormatter: dateFormatter) // 1525324500
let originsDate: Date? = dictionary.optionalDate("originDate", dateFormatter: dateFormatter) // Optional(1525324500)
let originsDate: Date = try dictionary.requiredDate("doesntExists", dateFormatter: dateFormatter) // throws error!!

Author

Niels Koole, Roadmap (Twitter)

License

EasyDictionary is available under the MIT license. See the LICENSE file for more info.