i have two different models with slightly different properties in my project. But when i use them without default values in one of the model, it gives error Type 'Instructions' does not conform to protocol 'Decodable' if i just remove = "" from the property summary inside Instructions model. Why this happens and how to overcome? Because in this way, even if i get data from API, it doesn't overwrite the default value.
INSTRUCTIONS MODEL
struct Instructions: Codable, Hashable, Equatable {
let vegetarian: Bool = false
let vegan: Bool = false
let glutenFree: Bool = false
let dairyFree: Bool = false
let veryHealthy: Bool = false
let cheap: Bool = false
let veryPopular: Bool = false
let sustainable: Bool = false
let lowFodmap: Bool = false
let weightWatcherSmartPoints: Int = 0
let gaps: String = ""
let preparationMinutes: Int = 0
let cookingMinutes: Int = 0
let aggregateLikes: Int = 0
let healthScore: Int = 0
let creditsText: String = ""
let license: String = ""
let sourceName: String = ""
let pricePerServing: Double = 0
let extendedIngredients: [ExtendedIngredient]
let id: Int
let title: String
let readyInMinutes: Int = 0
let servings: Int = 0
let sourceUrl: String = ""
let image: String
let imageType: String
let summary: String = ""
let cuisines: [String] = []
let dishTypes: [String] = []
let diets: [String] = []
let occasions: [String] = []
let winePairing: WinePairing?
let instructions: String = ""
let analyzedInstructions: [AnalyzedInstructions]
let originalId: Int? = 0
let spoonacularScore: Double = 0
let spoonacularSourceUrl: String = ""
enum CodingKeys: String, CodingKey {
case id, title, image, imageType, winePairing, analyzedInstructions, extendedIngredients
}
}
struct ExtendedIngredient: Codable, Hashable, Equatable {
let id: Int
let aisle: String
let image: String
let consistency: String
let name: String
let nameClean: String
let original: String
let originalName: String
let amount: Double
let unit: String
let meta: [String]
let measures: Measures
}
struct Measures: Codable, Hashable, Equatable {
let us: Measure
let metric: Measure
}
struct Measure: Codable, Hashable, Equatable {
let amount: Double
let unitShort: String
let unitLong: String
}
struct WinePairing: Codable, Hashable, Equatable {
let pairedWines: [String]
let pairingText: String
let productMatches: [ProductMatch]
}
struct ProductMatch: Codable, Hashable, Equatable {
}
struct AnalyzedInstructions: Codable, Hashable, Equatable {
let name: String
let steps: [Steps]
}
struct Steps: Codable, Hashable, Equatable {
let number: Int
let step: String
let ingredients: [Ingredients]
let equipment: [Equipments]
let length: Lengths?
}
struct Ingredients: Codable, Hashable, Equatable {
let id: Int
let name: String
let localizedName: String
let image: String
}
struct Equipments: Codable, Hashable, Equatable {
let id: Int
let name: String
let localizedName: String
let image: String
}
struct Lengths: Codable, Hashable, Equatable {
let number: Int
let unit: String
}
RECIPE MODEL
// MARK: - Welcome
struct RecipeResults: Codable {
let results: [Recipe]
let offset, number, totalResults: Int
}
// MARK: - Result
struct Recipe: Codable, Hashable, Equatable {
let vegetarian, vegan, glutenFree, dairyFree: Bool
let veryHealthy, cheap, veryPopular, sustainable: Bool
let lowFodmap: Bool
let weightWatcherSmartPoints: Int
let gaps: String
let preparationMinutes, cookingMinutes, aggregateLikes, healthScore: Int
let creditsText, sourceName: String
let pricePerServing: Double
let id: Int
let title: String
let readyInMinutes, servings: Int
let sourceURL: String
let image: String
let imageType, summary: String
let dishTypes, diets: [String]
let analyzedInstructions: [AnalyzedInstruction]
enum CodingKeys: String, CodingKey {
case vegetarian, vegan, glutenFree, dairyFree, veryHealthy, cheap, veryPopular, sustainable, lowFodmap, weightWatcherSmartPoints, gaps, preparationMinutes, cookingMinutes, aggregateLikes, healthScore, creditsText, sourceName, pricePerServing, id, title, readyInMinutes, servings
case sourceURL = "sourceUrl"
case image, imageType, summary, dishTypes, diets, analyzedInstructions
}
}
// MARK: - AnalyzedInstruction
struct AnalyzedInstruction: Codable, Hashable, Equatable {
let name: String
let steps: [Step]
}
// MARK: - Step
struct Step: Codable, Hashable, Equatable {
let number: Int
let step: String
let ingredients, equipment: [Ent]
let length: Length?
}
// MARK: - Ent
struct Ent: Codable, Hashable, Equatable {
let id: Int
let name, localizedName, image: String
let temperature: Length?
}
// MARK: - Length
struct Length: Codable, Hashable, Equatable {
let number: Int
let unit: String
}
You are using custom coding keys for properties in
InstructionsModel. In order to conform to toCodableprotocol you need to define all properties and their custom keys, or if your property names are the same as JSON keys, just remove that enum.