I am working on a cookbook app and I thought I had the knowledge to get the last bit up and running and I have run into a snag. I am trying to get my ingredients array to display on a text field and I cannot get it to work. I have done some research and I have tried a few different things and all of them come up with different errors. I have left them in the code but commented them out so maybe someone looking at it could see what I was doing wrong with what I had going. My code looks as follows for my view controller that will display the ingredients.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var ingredients: UITextView!
@IBOutlet weak var directions: UITextView!
var recipe : Recipe!
// var ingredientText = String()
// let separator = (" / ")
override func viewDidLoad() {
super.viewDidLoad()
self.title = recipe.name
directions.text = recipe.directions
// let ingredientsText = separator.join(recipe.ingredients)
// label.text = ingredientsText
// for ingredient: String in recipe.ingredients {
// ingredientsText.join("%@\n", ingredient)
// }
// label.text = ingredientText
// label.text = recipe.ingredients
// var multiLineString = join("\n", recipe.ingredients)
// label.text = multiLineString
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
this is the table VC
import UIKit
class tableVC: UITableViewController {
var recipes: [Recipe] = []
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Juicing Recipes"
let recipe0: Recipe = Recipe()
recipe0.name = "Number 1"
recipe0.ingredients = ["Pasta","Pasta Sauce","Hamburger"]
recipe0.directions = "Cook pasta to directions on box, brown hamburger, add sauce to hamburger, dump on pasta"
recipes.append(recipe0)
let recipe1: Recipe = Recipe()
recipe1.name = "Number 2"
recipe1.ingredients = ["all kinds of stuff","Then a dash of salt"]
recipe1.directions = "enjoy"
recipes.append(recipe1)
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return recipes.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = self.tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as UITableViewCell
let recipe: Recipe = recipes[indexPath.row]
cell.textLabel?.text = recipe.name
return cell
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let indexPath : NSIndexPath = self.tableView.indexPathForSelectedRow!
let destCon : ViewController = segue.destinationViewController as! ViewController
destCon.recipe = recipes[indexPath.row]
}
}
then the recipe class
class Recipe: NSObject {
var name = String()
var ingredients = [String]()
var directions = String()
}
It's another one of those things I know it something simple that I am overlooking but I have been stumped on this for a while.