EasyDictionary 0.2.4

EasyDictionary 0.2.4

Paul van RoosendaalNiels KooleRoadmap 维护。



EasyDictionary

CI Status Version License Platform Twitter

示例

要运行示例项目,请首先克隆仓库,然后从“示例”目录运行 pod install

所需条件

  • Swift 4.0

安装

EasyDictionary 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile 中。

pod 'EasyDictionary'

摘要

这个小助手提供了一些从你的字典中获取可选和非可选值的方法。它可以在任何地方使用,但我们主要用它来解析端点数据到对象中。

特点

  • 从字典中获取类型的一种通用方法。
  • 直接解析URL。
  • 直接解析日期。
  • 从日期字符串中解析时区。(当然,因为我们正在构建THE旅行应用 ;-)
  • 还有其他要求吗?

用法

获取数据

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在MIT许可下可用。更多信息请参阅LICENSE文件。