Skip to content

Instantly share code, notes, and snippets.

@alper
Last active September 3, 2018 16:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alper/dd282dd3104915ca1d7bbe96b2294fe1 to your computer and use it in GitHub Desktop.
Save alper/dd282dd3104915ca1d7bbe96b2294fe1 to your computer and use it in GitHub Desktop.
//
// Models.swift
// strw
//
// Created by Alper Cugun on 23/8/18.
// Copyright © 2018 alper. All rights reserved.
//
import Foundation
struct AnyCodable: Codable {}
struct Trending: Codable {
var data: [Gif]
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
var gifContainer = try container.nestedUnkeyedContainer(forKey: .data)
var gifs = [Gif]()
while !gifContainer.isAtEnd {
if let gif = try? gifContainer.decode(Gif.self) {
gifs.append(gif)
} else {
let skipped = try? gifContainer.decode(AnyCodable.self)
print("Skipping one \(skipped)")
}
}
self.data = gifs
}
}
struct Gif: Codable {
let title: String
let images: GifImageVariants
}
struct GifImageVariants: Codable {
let original: GifImage
let fixed_width_small: GifImage
}
struct GifImage: Codable {
let url: URL
// init(url: URL) {
// self.url = url
// }
// init(from decoder: Decoder) throws {
// let values = try decoder.container(keyedBy: CodingKeys.self)
//
// do {
// self.url = try values.decode(URL.self, forKey: .url)
// } catch {
// let raw = try values.decode(String.self, forKey: .url)
// print("Error decoding URL for value: '\(raw)'")
// }
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment