Skip to content

Instantly share code, notes, and snippets.

@intan1907
Created June 16, 2021 09:06
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 intan1907/faebd88790fa32521c16cecd4937c51e to your computer and use it in GitHub Desktop.
Save intan1907/faebd88790fa32521c16cecd4937c51e to your computer and use it in GitHub Desktop.
RecipeBaseClass with widgetURL property
public struct RecipeBaseClass: Codable, Hashable {
// ...
public var ingredients: [RecipeIngredients]?
public var originalURL: String?
public var imageURL: String?
public var steps: [String]?
public var name: String?
public var time: Int?
public var serving: String?
public var widgetURL: URL?
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
ingredients = try container.decodeIfPresent([RecipeIngredients].self, forKey: .ingredients)
originalURL = try container.decodeIfPresent(String.self, forKey: .originalURL)
imageURL = try container.decodeIfPresent(String.self, forKey: .imageURL)
steps = try container.decodeIfPresent([String].self, forKey: .steps)
name = try container.decodeIfPresent(String.self, forKey: .name)
time = try container.decodeIfPresent(Int.self, forKey: .time)
serving = try container.decodeIfPresent(String.self, forKey: .serving)
widgetURL = URL(string: "recipe://\((name ?? "").addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) ?? (name ?? "").filter { $0.isWhitespace })")
}
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment