Last active
September 3, 2018 16:49
-
-
Save alper/dd282dd3104915ca1d7bbe96b2294fe1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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