试图转换旧的Swift代码 - Cstring问题

时间:2015-07-07 08:37:24

标签: swift c-strings

我正在尝试使用其他人的代码作为我想要做的事情的基础。但它似乎是为旧版Swift编写的,所以我遇到了很多错误。我已经能够解决不少但现在我很难过。

我正在纠正的代码是:

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)
}

}

项目中还有其他问题,但这将给我一个良好的开端。

1 个答案:

答案 0 :(得分:0)

使用swift编程语言从sqlite数据库中检索数据

func retrivingDataFromSqliteDb(){

    var database:COpaquePointer = nil

    // This is for getting document directory path or database path
    let dbpath = appDelegate.getting_SandBox_Path().cStringUsingEncoding(NSUTF8StringEncoding)

    // open the database and checking open or not
    let error = sqlite3_open(dbpath, &database)

    if error != SQLITE_OK {

        println("Error while opening");
    }
    else{
        println("already database open");


        var selectQuery = "select * from emptab"

        var result:CInt = 0
        var stmt:COpaquePointer = nil

        result = sqlite3_prepare_v2(database, selectQuery, -1, &stmt, nil);
        if result != SQLITE_OK {

            println("failed : \(sqlite3_errmsg(database))")
        }
        else  {

            result = sqlite3_step(stmt)
             array_data.removeAllObjects()

            while result == SQLITE_ROW {
            var Dictionary = NSMutableDictionary()

            let fname = sqlite3_column_text(stmt, 0)
            let lname = sqlite3_column_text(stmt, 1)
            let phone = sqlite3_column_text(stmt, 2)
            let email = sqlite3_column_text(stmt, 3)
            let address = sqlite3_column_text(stmt, 4)

       // adding to retrived objects to dictionary
    Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(fname))!, forKey: "firstName")
    Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(lname))!, forKey: "LastName")
    Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(phone))!, forKey: "MobileNum")
    Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(email))!, forKey: "mail")
    Dictionary.setObject(String.fromCString(UnsafePointer<CChar>(address))!, forKey: "address")


                // dictionary of data adding to the Array
                array_data .addObject(Dictionary)

                result = sqlite3_step(stmt)

            }
            println(array_data)

        }

    }

}