Swift: Can't get rid of 'Optional' option when setting cell text label
I have spent quite a lot of time trying to solve this, but I have no joy.
I have an array (levelArrayName) created in my appDelegate that is populated with a sqlite database:
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var database:COpaquePointer = nil
var levelArray: [[Int]] = []
var levelArrayName: [[String]] = []
func checkdatabase(){
//check is database exist at path.....
var dirPaths:NSString
dirPaths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as! NSString;
let databasePath = dirPaths.stringByAppendingString("/rugby.sqlite")
let dbpath = databasePath.cStringUsingEncoding(NSUTF8StringEncoding)
let filemanager = NSFileManager.defaultManager()
if (!filemanager.fileExistsAtPath(databasePath))
{
let fileForCopy = NSBundle.mainBundle().pathForResource("rugby",ofType:"sqlite")
filemanager.copyItemAtPath(fileForCopy!, toPath:databasePath, error: nil)
}
let error = sqlite3_open(dbpath!, &database)
if error != SQLITE_OK
{
println("error while opening");
sqlite3_close(database);
}
}
func getLevels() {
var selectQuery="SELECT * FROM level"
var stmt:COpaquePointer = nil
var result = sqlite3_prepare_v2(database, selectQuery, -1, &stmt, nil);
if result != SQLITE_OK
{
println("failed \(sqlite3_errmsg(database))")
println("failed \(sqlite3_errcode(database))")
}
else
{
result = sqlite3_step(stmt)
while result == SQLITE_ROW {
var levelID = Int(sqlite3_column_int(stmt, 0))
var attackID = Int(sqlite3_column_int(stmt, 2))
var defenceID = Int(sqlite3_column_int(stmt, 2))
var runs = Int(sqlite3_column_int(stmt, 3))
var tries = Int(sqlite3_column_int(stmt, 4))
var name_buf = sqlite3_column_text(stmt, 5)
var name = String.fromCString(UnsafePointer<CChar>(name_buf))
result = sqlite3_step(stmt)
self.levelArray += [[levelID, attackID, defenceID, runs, tries]]
self.levelArrayName += [["\(name)"]]
}
}
}
Then I use this array in one of my tableview controllers to set the cell label:
import Foundation
import UIKit
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
class LevelsViewController: UITableViewController, UITableViewDelegate, UITableViewDataSource {
var levelArrayName = appDelegate.levelArrayName
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "cell")
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return appDelegate.levelArray.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("LevelCell", forIndexPath: indexPath) as! UITableViewCell
cell.textLabel?.text = levelArrayName[indexPath.row][0]
return cell
//return UITableViewCell()
}
However, the cell label shows "Optional:" before the string from the array.
I've tried expanding the appDelegate.levelArrayName but I can't seem to get this to work.
I'm sure I'm missing something lightweight, but it would be helpful if it points in the right direction.
thank
source to share
For anyone else who had the same problem like me, I was able to work out an answer that helped a lot on this thread: Can't call 'append' using an argument list like '(String ?!) "
In AppDelegate, I changed my code:
self.levelArrayName += [["\(name)"]]
For
if let nameArray = name as String! {self.levelArrayName.append(name!)}
And then there is no need to unwrap in the tableviewcontroller, so this code worked:
cell.textLabel?.text = levelArrayName[indexPath.row]
This may seem like an easy answer, but I hope it helps others.
source to share