gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:15:15 25 4
gpt4 key购买 nike

我正在尝试使用别人的代码作为我想做的事情的基础。然而它似乎是为旧版本的 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)
}

}

该项目中还存在其他问题,但这会给我一个良好的开端。

最佳答案

使用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)

}

}

}

关于swift - 尝试转换旧的 Swift 代码 - Cstring 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31263672/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com