Skip to content

Instantly share code, notes, and snippets.

@bestiosdeveloper
Last active January 30, 2021 19:19
Show Gist options
  • Save bestiosdeveloper/6abfc24d106549aa0d55bef7ac316176 to your computer and use it in GitHub Desktop.
Save bestiosdeveloper/6abfc24d106549aa0d55bef7ac316176 to your computer and use it in GitHub Desktop.
let defaultText: String = "N/A"
struct Article: Hashable {
var sourceId: String = defaultText
var sourceName: String = defaultText
var author: String = defaultText
var title: String = defaultText
var description: String = defaultText
var url: String = defaultText
var urlToImage: String = defaultText
var publishedAt: String = defaultText
var content: String = defaultText
//"2019-06-07T10:37:36Z"
var publishDate: Date? {return self.publishedAt.toDate(dateFormat: "yyyy-MM-dd'T'HH:mm:ssZ")}
var imageUrl: URL? {return URL(string: urlToImage)}
init(json: JsonDictionary) {
if let source = json["source"] as? JsonDictionary {
if let obj = source["id"] {self.sourceId = "\(obj)"}
if let obj = source["name"] {self.sourceName = "\(obj)"}
}
if let obj = json["author"] {self.author = "\(obj)"}
if let obj = json["title"] {self.title = "\(obj)"}
if let obj = json["description"] {self.description = "\(obj)"}
if let obj = json["url"] {self.url = "\(obj)"}
if let obj = json["urlToImage"] {self.urlToImage = "\(obj)"}
if let obj = json["publishedAt"] {self.publishedAt = "\(obj)"}
if let obj = json["content"] {self.content = "\(obj)"}
}
static func getModels(json: [JsonDictionary]) -> [Article] {return json.map { Article(json: $0) }}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment