I have a struct, and have created a button in which I want to write something and then append the writing to my struct. I'm having trouble with how to write a correct code so that I append correctly the text from the button into my struct.
here is my struct:
struct Candy{
let category : String
let name : String
}
candies = [
Candy(category:"test", name:"test 2"),
Candy(category:"test", name:"test 4"),
Candy(category:"test", name:"test 1"),
Candy(category:"Music", name:"J-cole"),
Candy(category:"Music", name:"Jay-z"),
Candy(category:"Music", name:"Coldplay"),
Candy(category:"Other", name:"Sophie"),
Candy(category:"Other", name:"Frederic"),
Candy(category:"Other", name:"test")]
}
And this is the code for my button:
@IBAction func addBar(sender: AnyObject) {
let alert = UIAlertController(title: "New name",
message: "Write name",
preferredStyle: .Alert)
let saveAction = UIAlertAction(title: "Save",
style: .Default,
handler: { (action:UIAlertAction) -> Void in
let textField = alert.textFields!.first
self.candies.append(textField!.text!) // I have troubles with this line... not sure how to make this line correct..
self.tableView.reloadData()