Reputation: 29
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
}
Upvotes: 0
Views: 49
Reputation: 285059
The error is clear : ingredients
is declared as String
but in the Singleton an array ([String]
) is passed. The name implies an array yet.
And the private(set)
variables might look pretty cool but they are nonsense. This is not Objective-C. If you want constants declare them as constants (let
)
This is sufficient, you get the initializer for free:
struct RecipeInfo {
let image: String
let title: String
let time: String
let complication: String
let serving: String
let ingredients: [String]
let instructions: String
}
To display ingredients
in the table view join the array
ingredients.text = recipe.ingredients.joined(separator: ", ")
Upvotes: 1