2

I am using a function that takes an UnsafePointer<String>.

How do I get an UnsafePointer<String> from a String?

Trying &someString gives me the error:

'inout String' is not convertible to 'UnsafePointer<String>'
JuJoDi
  • 14,627
  • 23
  • 80
  • 126

1 Answers1

2

&someString creates a CMutablePointer<String>.

To convert it to an UnsafePointer, the simplest solution is to call the special function:

withUnsafePointer(&someString) { unsafePointer in
    //do something with the unsafePointer
}
Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • 2
    Strangely, now this produces `Error:(26, 27) 'String' is not convertible to '@lvalue inout $T4'` if the String is a constant, or `Error:(26, 9) cannot convert the expression's type '(inout String, (($T6) -> ($T6) -> $T5) -> (($T6) -> $T5) -> $T5)' to type "Result"` if it's a variable – Dan Rosenstark Dec 24 '14 at 14:19