在Swift的SQLite项目中使用UnsafePointer <Uint8>问题

我们正在Swift中实现iOS中的SQLite,而不使用包装器或Objective-C桥接。 一切工作正常,除了做一个查询和提取结果。 问题出在Swift中从SQLite返回的UnsafePointer<UInt8> ,如下所示:

 var querySQL = "SELECT address, phone FROM CONTACTS WHERE NAME = 'myName'" var cQuery = querySQL.cStringUsingEncoding(NSUTF8StringEncoding) var statement: COpaquePointer = nil if sqlite3_prepare_v2(contactsDB, cQuery!, -1, &statement, nil) == SQLITE_OK { if sqlite3_step(statement) == SQLITE_ROW { var address : UnsafePointer<UInt8> = sqlite3_column_text(statement, 0) var data = NSData(bytes: address, length: 10) var string = NSString(data: data, encoding: NSUTF8StringEncoding) println(string) 

正如你所看到的,如果我们知道对象的长度(在本例中为10),我们可以将指针转换为String,

为了深入研究这个问题,我有下面的例子

 let pointerFromString: UnsafePointer<Int8> = "xyz".cStringUsingEncoding(NSUTF8StringEncoding) let stringFromPointer = String.fromCString(anotherPointerFromString_Int8) println(stringFromPointer!) 

鉴于CCharCChar的别名,我可以使用.cStringUsingEncoding()String转换为UnsafePointer<Int8> ,然后使用.fromCString(<UnsafePointer_CChar>)String转换为String

问题是我的SQLite结果是一个UnsafePointer_UInt8 ,不能用于.fromCString()

底线的问题是:是否有可能将UnsafePointer_UInt8转换或转换为UnsafePointer_Int8

这应该工作:

 let address = sqlite3_column_text(statement, 0) let string = String.fromCString(UnsafePointer<CChar>(address)) 

更新Swift 3(Xcode 8),比较Swift 3:将一个以null结尾的UnsafePointer <UInt8>转换为一个string :

 let string = String(cString: sqlite3_column_text(statement, 0))