I am new to swift and what I am trying to do is return the specified columns in my table to display in a separate viewcontroller. I have defined a function using SQLite.swift which returns them in an array, just like I want them to be. (It works when called in the same viewcontroller)
func returncolumns() -> Array<String> {
print("RETURNING")
var namearray = [String]()
do {
for wardrobe in try wardrobedb.prepare(wardrobe.select(name)) {
namearray.append(wardrobe[name])
}
} catch {
print("This no worko \(error)")
}
return namearray
}
But when I call the function from another viewcontroller, I get a fatal error due to it trying to unwrap a nil value.
var clothes = ViewController().returncolumns()
What I gather is that since I am calling the function from the viewcontroller without the table, it is not able to get the information that it needs.
Here is the database in the first viewcontroller
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var wardrobedb: Connection!
let wardrobe = Table("wardrobe")
let id = Expression<Int64>("id")
let name = Expression<String>("name")
let type = Expression<String>("type")
let color = Expression<String>("color")
let style = Expression<String>("style")
let weight = Expression<String>("weight")
let pattern = Expression<String>("pattern")
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
do {
let documentDirectory = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
let fileUrl = documentDirectory.appendingPathComponent("wardrobe").appendingPathExtension("sqlite3")
let wardrobedb = try Connection(fileUrl.path)
// let wardrobedb = try remove(fileUrl.path)
self.wardrobedb = wardrobedb
} catch {
print(error)
}
}
So should I try to combine the view controllers so that its all accessible? or move the wardrobe instantiation to another class? or is there a nice simple addition I can make to the function call which will allow for the table to be accessed.
Thank you!
Second ViewController:
import UIKit
class WardrobeTableViewController: UITableViewController {
var clothes = [String]()
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return clothes.count
}
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "Section \(section)"
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "LabelCell", for: indexPath)
let clothesname = clothes[indexPath.row]
cell.textLabel?.text = clothes[indexPath.row]
cell.detailTextLabel?.text = "Very cool!"
// cell.imageView?.image = UIImage(named: clothesname)
return cell
}
}
Here is first viewcontroller calls
func returncolumns() -> Array<String> {
print("RETURNING")
var namearray = [String]()
do {
for wardrobe in try wardrobedb.prepare(wardrobe.select(name)) {
namearray.append(wardrobe[name])
}
} catch {
print("This no worko \(error)")
}
return namearray
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "wardrobemove" {
// if let destinationVC = segue.destination as? WardrobeTableViewController {
WardrobeTableViewController().clothes = returncolumns()
/// }
}
}
So here I am identifying "wardrobemove" as the segue which moves into a navigation controller. I commented out the if statement because in this instance the destination viewcontroller is the navigation controller not the WardrobeTableViewController where the clothes array is being stored. When I run the debugger, I see it passing the information and the clothes array being assigned. but it still disappears before that.
Between the Navigation controller and the WardrobeTableViewController there is a Table view controller which has a few cells and when the first one is selected, it opens the WardrobeTableViewController.
This is a bit of a mess, I have followed two guides and I think the second one bringing in the navigation controller has messed things up a bit.