swift 3 UnsafePointer sqlite

Hi all,


I use the below code to get a value from a recordset in sqlite:


let colSQL0 = sqlite3_column_text(compiledStatement, 0) 
let value = String(cString: UnsafePointer<CChar>(colSQL0!))


that is working fine, but now in Swift 3 appears the following error getting the value:


error: 'init' is unavailable: use 'withMemoryRebound(to:capacity:_)' to temporarily


any suggestion? thank you in advance...

Accepted Reply

You just need to delete the

UnsafePointer
creation.
let value = String(cString: colSQL0!)

Two points:

  • You can no longer convert from pointers of one type to pointers of another type directly. This is fallout from SE-0107 UnsafeRawPointer API, which puts Swift’s type aliasing rules on a firm footing.

  • In this case that’s not a problem because, as a special case, Swift 3 support constructing a

    String
    from a C string using either signed or unsigned characters

Specifically, there are two constructors:

public init(cString: UnsafePointer<CChar>)
public init(cString: UnsafePointer<UInt8>)

and, as noted in the comments, the second “is identical to

init(cString: UnsafePointer<CChar>)
but operates on an unsigned sequence of bytes.”

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

You just need to delete the

UnsafePointer
creation.
let value = String(cString: colSQL0!)

Two points:

  • You can no longer convert from pointers of one type to pointers of another type directly. This is fallout from SE-0107 UnsafeRawPointer API, which puts Swift’s type aliasing rules on a firm footing.

  • In this case that’s not a problem because, as a special case, Swift 3 support constructing a

    String
    from a C string using either signed or unsigned characters

Specifically, there are two constructors:

public init(cString: UnsafePointer<CChar>)
public init(cString: UnsafePointer<UInt8>)

and, as noted in the comments, the second “is identical to

init(cString: UnsafePointer<CChar>)
but operates on an unsigned sequence of bytes.”

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

thank you!