SQLITE_TRANSIENT undefined in Swift

白昼怎懂夜的黑 提交于 2019-12-17 09:43:50

问题


I'm using xcode 6 and I've imported libsqlite3.dylib and libsqlite3.0.dylib. I've also added the Bridging-Header.h file witch imports sqlite3.h

I can open SQLite database and do simple operations like insert select...

With if (sqlite3_bind_text(compiledStatement, 2, Name.cStringUsingEncoding(NSUTF8StringEncoding), -1, SQLITE_TRANSIENT) != SQLITE_OK)

I have an error: Use of unresolved identifier 'SQLITE_TRANSIENT'

What show I do? I'm new in Swift, it's my first question on Stack, pls somebody help me!


回答1:


The definitions

#define SQLITE_STATIC      ((sqlite3_destructor_type)0)
#define SQLITE_TRANSIENT   ((sqlite3_destructor_type)-1)

from <sqlite3.h> are not imported to Swift, probably due to the "unsafe" pointer casting.

A possible Swift definition is shown in the SQLite.swift project, in Statement.swift:

let SQLITE_STATIC = sqlite3_destructor_type(COpaquePointer(bitPattern: 0))
let SQLITE_TRANSIENT = sqlite3_destructor_type(COpaquePointer(bitPattern: -1))

For Swift 2 you will need

let SQLITE_STATIC = unsafeBitCast(0, sqlite3_destructor_type.self)
let SQLITE_TRANSIENT = unsafeBitCast(-1, sqlite3_destructor_type.self)

(taken from "Helpers.swift" from the Swift 2 branch of the SQLite.swift project).

Update for Swift 3:

let SQLITE_STATIC = unsafeBitCast(0, to: sqlite3_destructor_type.self)
let SQLITE_TRANSIENT = unsafeBitCast(-1, to: sqlite3_destructor_type.self)


来源:https://stackoverflow.com/questions/26883131/sqlite-transient-undefined-in-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!