I have a swift file currently using as a singleton which will eventually
be the location that I use to grab all the API data, I have a view file for my iboulets and a function to update the views for the tableview cells, along with a model file that has my variables and initializer. Everything works fine until I tried to pass through an array instead of a string, can you assist me in passing an array or arrays through my update views function.Current error
Singleton
class DataServices{
static let instance = DataServices()
private let ackee = [
RecipeInfo(image: "15", title: "Ackee & Saltfish", time: "30 mins", complication: "Easy", serving: "5 people", ingredients: ["Canned Ackee", "Black Pepper", "Salt Fish"], instructions: "again")
]
func getRecipe() -> [RecipeInfo]{
return ackee
}
Update View function for the cell
class RecipeCell: UITableViewCell {
@IBOutlet weak var recipeImage: UIImageView!
@IBOutlet weak var recipeTitle: UILabel!
@IBOutlet weak var recipeTime: UILabel!
@IBOutlet weak var recipeStatus: UILabel!
@IBOutlet weak var mealCount: UILabel!
@IBOutlet weak var ingredients: UILabel!
@IBOutlet weak var instructions: UILabel!
func updateViews(recipe: RecipeInfo){
recipeImage.image = UIImage(named: recipe.image)
recipeTitle.text = recipe.title
recipeTime.text = recipe.time
recipeStatus.text = recipe.complication
mealCount.text = recipe.serving
ingredients.text = recipe.ingredients
instructions.text = recipe.instructions
}
Model with variables and initializer
struct RecipeInfo {
private(set) public var image: String
private(set) public var title: String
private(set) public var time: String
private(set) public var complication: String
private(set) public var serving: String
private(set) public var ingredients: String
private(set) public var instructions: String
init(image: String, title: String, time: String, complication: String, serving: String, ingredients: String, instructions: String) {
self.image = image
self.title = title
self.time = time
self.complication = complication
self.serving = serving
self.ingredients = ingredients
self.instructions = instructions
}
ingredients: Stringwithingredients: [String]instead. For single ingredient (only a String, just doRecipeInfo(image... ingredients: [myIngredient],...)For the updateView, it depends how you want to show multiple ingredients, only first?ingredients.text = recipe.ingredients.firstoringredients.text = recipe.ingredients.joined(separator: ", "), etc.initmethod in yourRecipeInfostruct. You get that exactinitfor free. And change all of the properties to belet. No need forvaror theprivate (set) public.