To run the example project, clone the repo, and run pod install
from the Example directory first.
- Swift 4.0
EasyDictionary is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'EasyDictionary'
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.
- 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?
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!!
Niels Koole, Roadmap (Twitter)
EasyDictionary is available under the MIT license. See the LICENSE file for more info.