0

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
    }
4
  • Replace ingredients: String with ingredients: [String] instead. For single ingredient (only a String, just do RecipeInfo(image... ingredients: [myIngredient],...) For the updateView, it depends how you want to show multiple ingredients, only first? ingredients.text = recipe.ingredients.first or ingredients.text = recipe.ingredients.joined(separator: ", "), etc. Commented Aug 21, 2018 at 16:13
  • You're awesome, that solved my issue big time. However the array isn't displaying the list as I wish it would. I am trying to have the ingredients in separate cells Commented Aug 21, 2018 at 16:30
  • FYI - There is no need for the init method in your RecipeInfo struct. You get that exact init for free. And change all of the properties to be let. No need for var or the private (set) public. Commented Aug 21, 2018 at 17:03
  • I was only doing it the way I was taught Commented Aug 21, 2018 at 17:08

1 Answer 1

1

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: ", ")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.