2014-11-12 10 views
12

Sto usando xcode 6 e ho importato libsqlite3.dylib e libsqlite3.0.dylib. Ho anche aggiunto le Bridging-Header.h di file streghe importazioni sqlite3.hSQLITE_TRANSIENT undefined in Swift

posso aprire il database SQLite e fare operazioni semplici come inserto selezionare ...

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

ho un errore: Use of unresolved identifier 'SQLITE_TRANSIENT'

Che spettacolo faccio? Sono nuovo in Swift, è la mia prima domanda su Stack, qualcuno mi aiuta!

+0

mostrare un po 'di codice come come si importano sqlite3.h? –

+0

nel mio Bridging-Header.h. Aggiungo #import . Penso che sia perché nel mio sqlite3.h non definisce SQLITE_TRANSIENT e SQLITE_STATIC perché posso usare altre costanti come SQLITE_OK, SQLITE_DONE. – Chongzl

+0

sì, l'errore lo dice semplicemente. –

risposta

15

Le definizioni

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

da <sqlite3.h> non vengono importati a Swift, probabilmente a causa della "non sicuro" puntatore casting.

Una possibile definizione Swift è mostrato nel progetto SQLite.swift, in Statement.swift:

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

Per Swift 2 dovrai

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

(tratto dal "Helpers.swift" dalla Swift 2 ramo del progetto SQLite.swift).

Aggiornamento per Swift 3:

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

Ha funzionato! Grazie! – Chongzl

+0

cool, la versione swift 3 funziona anche in swift 4! – hasen