list – 如何在swift中将回调函数传递给sqlite3_exec?

list – 如何在swift中将回调函数传递给sqlite3_exec?,第1张

概述如何在 Swift中将回调函数传递给sqlite3_exec? sqlite_str = sqlite_str + "\(sqlite_property_str))";var str:NSString = sqlite_str;var sqlite:COpaquePointer = share().sqlite3_db;var errmsg:UnsafePointer<Int8> = n 如何在 Swift中将回调函数传递给sqlite3_exec?

sqlite_str = sqlite_str + "\(sqlite_property_str))";var str:Nsstring = sqlite_str;var sqlite:copaquePointer = share().sqlite3_db;var errmsg:UnsafePointer<Int8> = nillet rc = sqlite3_exec(sqlite,str.cStringUsingEnCoding(NSUTF8StringEnCoding),<#callback: CFunctionPointer<((UnsafePointer<()>,Int32,UnsafePointer<UnsafePointer<Int8>>,UnsafePointer<UnsafePointer<Int8>>) -> Int32)>#>,<#UnsafePointer<()>#>,<#errmsg: UnsafePointer<UnsafePointer<Int8>>#>)
解决方法 Swift 2.2提供了两个用于实现sqlite3_exec回调函数的选项:(1)全局非实例func过程或(2)非捕获文字{}闭包.

sqlite.org的“SQLite in 5 minutes or less”示例在Swift Xcode7项目here中实现.

可读的类型

typealias sqlite3 = copaquePointertypealias CCharHandle = UnsafeMutablePointer<UnsafeMutablePointer<CChar>>typealias CCharPointer = UnsafeMutablePointer<CChar>typealias CVoIDPointer = UnsafeMutablePointer<VoID>

回调方法

func callback(    resultVoIDPointer: CVoIDPointer,// voID *NotUsed     columnCount: CInt,// int argc    values: CCharHandle,// char **argv         columns: CCharHandle             // char **azColname    ) -> CInt {    for  i in 0 ..< Int(columnCount) {        guard let value = String.fromCString(values[i])         else { continue }        guard let column = String.fromCString(columns[i])         else { continue }        print("\(column) = \(value)")    }    return 0 // status ok}func sqlqueryCallbackBasic(argc: Int,argv: [String]) -> Int {    var db: sqlite3 = nil     var zErrMsg:CCharPointer = nil    var rc: Int32 = 0 // result code    if argc != 3 {        print(String(format: "ERROR: Usage: %s DATABASE sql-STATEMENT",argv[0]))        return 1    }    rc = sqlite3_open(argv[1],&db)    if  rc != 0 {        print("ERROR: sqlite3_open " + String.fromCString(sqlite3_errmsg(db))! ?? "" )        sqlite3_close(db)        return 1    }    rc = sqlite3_exec(db,argv[2],callback,nil,&zErrMsg)    if rc != sqlITE_OK {        print("ERROR: sqlite3_exec " + String.fromCString(zErrMsg)! ?? "")        sqlite3_free(zErrMsg)    }    sqlite3_close(db)    return 0}

关闭方法

func sqlqueryClosureBasic(argc argc: Int,argv: [String]) -> Int {    var db: sqlite3 = nil     var zErrMsg:CCharPointer = nil    var rc: Int32 = 0    if argc != 3 {        print(String(format: "ERROR: Usage: %s DATABASE sql-STATEMENT",&db)    if  rc != 0 {        print("ERROR: sqlite3_open " + String.fromCString(sqlite3_errmsg(db))! ?? "" )        sqlite3_close(db)        return 1    }    rc = sqlite3_exec(        db,// database         argv[2],// statement        {        // callback: non-capturing closure            resultVoIDPointer,columnCount,values,columns in            for i in 0 ..< Int(columnCount) {                guard let value = String.fromCString(values[i])                 else { continue }                guard let column = String.fromCString(columns[i])                 else { continue }                print("\(column) = \(value)")            }            return 0        },&zErrMsg    )    if rc != sqlITE_OK {        let errorMsg = String.fromCString(zErrMsg)! ?? ""        print("ERROR: sqlite3_exec \(errorMsg)")        sqlite3_free(zErrMsg)    }    sqlite3_close(db)    return 0}
总结

以上是内存溢出为你收集整理的list – 如何在swift中将回调函数传递给sqlite3_exec?全部内容,希望文章能够帮你解决list – 如何在swift中将回调函数传递给sqlite3_exec?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1016696.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存