试图转换旧Swift代码-Cstring问题
我试图使用别人的代码作为我想做的事情的基础。然而,它似乎是为旧版本的Swift编写的,因此我得到了很多错误。我已经修好了不少,但现在我被难倒了 我正在更正的代码是:试图转换旧Swift代码-Cstring问题,swift,c-strings,Swift,C Strings,我试图使用别人的代码作为我想做的事情的基础。然而,它似乎是为旧版本的Swift编写的,因此我得到了很多错误。我已经修好了不少,但现在我被难倒了 我正在更正的代码是: import UIKit class ViewController: UIViewController { let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate var array_data: NSMutableArray = [
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)
}
}
导入UIKit
类ViewController:UIViewController{
让appDelegate=UIApplication.sharedApplication()。委托为!appDelegate
变量数组_数据:NSMutableArray=[]
@IBOUTLE var表_数据:UITableView?
重写func viewDidLoad(){
super.viewDidLoad()
selectFunc()
self.navigationItem.title=“Empolyee列表”
var addBtn=uibarbuttonite(标题:“添加”,样式:uibarbuttonimstyle.Plain,目标:self,操作:选择器(“insertFunc”))
self.navigationItem.rightBarButtonItem=addBtn
}
覆盖功能视图将出现(动画:Bool){
超级。视图将显示(动画)
selectFunc()
}
重写函数didReceiveMemoryWarning(){
超级。我收到了记忆警告()
//处置所有可以重新创建的资源。
}
//我在下面的代码块上做了一些工作,以避免使用Cstring(已弃用),这导致了下面的问题
func selectFunc(){
var selectQuery=“从员工信息中选择*
变量结果:CInt=0
var stmt:copaquepeninter=nil
结果=sqlite3\u prepare\u v2(appDelegate.database,selectQuery,-1,&stmt,nil);
如果结果!=SQLITE\u正常
{
println(“失败\(sqlite3_errmsg(appDelegate.database))”)
}
其他的
{
结果=sqlite3_步骤(stmt)
数组_data.removeAllObjects()
而结果==SQLITE\u行{
var Dictionary=NSMutableDictionary()
让name=sqlite3\u列\u文本(stmt,0)
设dep=sqlite3\u列\u文本(stmt,1)
让sal=sqlite3\u列\u文本(stmt,2)
//这是错误开始的地方-接下来的三行
Dictionary.setObject(String.fromCString(CString(name)),forKey:“name”)
Dictionary.setObject(String.fromCString(CString(dep)),forKey:“department”)
Dictionary.setObject(String.fromCString(CString(sal)),forKey:“salary”)
//错误(毫不奇怪)是“使用未解析的标识符'Cstring'”
//对那些遇到这个问题的人来说。。。
//使用…Dictionary.setObject(String.fromCString(UnsafePointer(name))!,forKey:“name”)
Dictionary.setObject(String.fromCString(UnsafePointer(dep))!,forKey:“department”)
Dictionary.setObject(String.fromCString(UnsafePointer(sal))!,forKey:“salary”)
数组\u data.addObject(字典)
结果=sqlite3_步骤(stmt)
}
}
self.table_data!.reloadData()
}
func insertFunc(){
让vc:UIViewController=storyboard!。将eviewcontrollerwhiteIdentifier(“AddViewController”)实例化为!UIViewController;
self.navigationController!.pushViewController(vc,动画:true)
}
func numberOfSectionsInTableView(tableView:UITableView!)->Int{
返回1
}
func tableView(tableView:UITableView!,numberofrowsinssection:Int)->Int{
返回self.array\u data.count
}
func tableView(tableView:UITableView!,cellForRowAtIndexPath:nsindepath!)->UITableView单元格{
//单细胞
var cell=tableView.dequeueReusableCellWithIdentifier(“cell”)作为?UITableViewCell
如果!(单元格!=nil){
单元格=UITableViewCell(样式:UITableViewCellStyle.Value1,reuseIdentifier:“单元格”)
}
var dic=array_data.objectAtIndex(indexPath.row)as!NSDictionary
cell!.textLabel!.text=dic.valueForKey(“name”)as!String
返回单元
}
func tableView(tableView:UITableView!,didSelectRowAtIndexPath:nsindepath!){
让vc=storyboard!。实例化eviewcontrollerwhiteIdentifier(“update_deleteViewController”)作为update_deleteViewController;
vc.dictionary=array\u data.objectAtIndex(indexPath.row)作为NSDictionary
self.navigationController.pushViewController(vc,动画:true)
}
}
项目中还有其他问题,但这将给我一个良好的开端。使用swift编程语言从sqlite数据库检索数据 func retrievingDatafromSqliteDB()函数{
var数据库:COpaquePointer=nil
//这用于获取文档目录路径或数据库路径
让dbpath=appDelegate.get_SandBox_Path().cStringUsingEncoding(NSUTF8StringEncoding)
//打开数据库并检查是否打开
让error=sqlite3\u打开(dbpath和数据库)
如果错误!=SQLITE\u正常{
println(“打开时出错”);
}
否则{
println(“已打开数据库”);
var selectQuery=“从空选项卡中选择*
变量结果:CInt=0
var stmt:copaquepeninter=nil
结果=sqlite3\u prepare\u v2(数据库,selectQuery,-1,&stmt,nil);
如果结果!=SQLITE\u正常{
println(“失败:\(sqlite3\u errmsg(数据库))”)
}
否则{
结果=sqlite3_步骤(stmt)
数组_data.removeAllObjects()
而结果==SQLITE\u行{
var Dictionary=NSMutableDictionary()
设fname=sqlite3\u column\u text(stmt,0)
让lname=sqlite3\u列\u文本(stmt,1)
let phone=sqlite3\u column\u text(stmt,2)
让email=sqlite3\u column\u text(stmt,3)
let address=sqlite3\u column\u text(stmt,4)
//将检索到的对象添加到字典
Dictionary.setObject(String.fromCString(UnsafePointer(fname))!,forKey:“firstName”)
Dictionary.setObject(String.fromCString(UnsafePointer(lname))!,forKey:“LastName”)
Dictionary.setObject(String.fromCString(UnsafePointer(phone))!,forKey:“MobileNum”)
Dictionary.setObject(String.fromCString(UnsafePointer(email))!,forKey:“mail”)
Dictionary.setObject(String.fromCString(UnsafePointer(address))!,forKey:“address”)
//添加到数组的数据字典
数组\u data.addObject(字典)
结果=sqlite3_步骤(stmt)
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)
}
}
}