I am trying to use someone else's code as a basis for what I want to do. However it appears to have been written for an older version of Swift and so I get lots of errors. I have been able to fix quite a few but now I am stumped.
Code I am correcting is:
import UIKit
class ViewController: UIViewController {
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
var array_data: NSMutableArray = []
@IBOutlet var table_data : UITableView?
override func viewDidLoad() {
super.viewDidLoad()
selectFunc()
self.navigationItem.title="Empolyee List"
var addBtn = UIBarButtonItem(title: "Add", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("insertFunc"))
self.navigationItem.rightBarButtonItem = addBtn
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
selectFunc()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// I did some work on the block below to get rid of the use of Cstring (Deprecated) that has caused issues below
func selectFunc(){
var selectQuery="select * from EmployeInfo"
var result:CInt=0
var stmt:COpaquePointer = nil
result = sqlite3_prepare_v2(appDelegate.database, selectQuery, -1, &stmt, nil);
if result != SQLITE_OK
{
println("failed \(sqlite3_errmsg(appDelegate.database))")
}
else
{
result = sqlite3_step(stmt)
array_data.removeAllObjects()
while result == SQLITE_ROW {
var Dictionary = NSMutableDictionary()
let name = sqlite3_column_text(stmt, 0)
let dep = sqlite3_column_text(stmt, 1)
let sal = sqlite3_column_text(stmt, 2)
// This is where the errors start - next three lines
Dictionary.setObject(String.fromCString(CString(name)), forKey:"name")
Dictionary.setObject(String.fromCString(CString(dep)), forKey: "department")
Dictionary.setObject(String.fromCString(CString(sal)), forKey: "salary")
// The error (not surprisingly) is "Use of Unresolved identifier 'Cstring'"
//OK for those who run into this issue...
//use... Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(name))!, forKey: "name")
Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(dep))!, forKey: "department")
Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(sal))!, forKey: "salary")
array_data .addObject(Dictionary)
result = sqlite3_step(stmt)
}
}
self.table_data!.reloadData()
}
func insertFunc(){
let vc : UIViewController = storyboard!.instantiateViewControllerWithIdentifier("AddViewController") as! UIViewController;
self.navigationController!.pushViewController(vc, animated: true)
}
func numberOfSectionsInTableView(tableView: UITableView!) -> Int {
return 1
}
func tableView(tableView: UITableView!, numberOfRowsInSection section: Int) -> Int {
return self.array_data.count
}
func tableView(tableView: UITableView!, cellForRowAtIndexPath indexPath: NSIndexPath!) -> UITableViewCell! {
//simple cell
var cell = tableView.dequeueReusableCellWithIdentifier("CELL") as? UITableViewCell
if !(cell != nil) {
cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier: "CELL")
}
var dic=array_data.objectAtIndex(indexPath.row) as! NSDictionary
cell!.textLabel!.text = dic.valueForKey("name") as! String
return cell
}
func tableView(tableView: UITableView!, didSelectRowAtIndexPath indexPath: NSIndexPath!){
let vc = storyboard!.instantiateViewControllerWithIdentifier("update_deleteViewController") as update_deleteViewController;
vc.dictionary=array_data.objectAtIndex(indexPath.row) as NSDictionary
self.navigationController.pushViewController(vc, animated: true)
}
}
There are other issues in the project but this will give me a good start.