Firstly, I'm very new to Swift, so keep that in mind before you berate my question for being an easy problem to solve.
I'm trying to use a button to send the contents of text field to a table. This bit of code defines the variable and you can see the sender:
import UIKit
var bookTitle:String!
class secondViewController: UIViewController {
@IBOutlet weak var titleField: UITextField!
@IBAction func addBook(sender: AnyObject) {
bookTitle = (titleField.text)!
}
This is the code where I'm receiving the error message:
import UIKit
var cellContent = ["Book 1", "Book 2", "Book 3", "Book 4"]
class firstViewController: UIViewController, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
cellContent.insert(bookTitle, atIndex: 0)
//THIS IS THE ERROR: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cellContent.count
}
From searching online, I think the problem is that the variable "bookTitle" is nil, but I'm not sure how to fix that.