Skip to content

Instantly share code, notes, and snippets.

@dduan
Last active August 27, 2018 20:19
Show Gist options
  • Save dduan/20534149ca788882475ae0c5925a3eba to your computer and use it in GitHub Desktop.
Save dduan/20534149ca788882475ae0c5925a3eba to your computer and use it in GitHub Desktop.
A function that turns a Swift 3 [String] to an array of C strings (char *[]).
func withCStrings(_ strings: [String], scoped: ([UnsafeMutablePointer<CChar>?]) throws -> Void) rethrows {
let cStrings = strings.map { strdup($0) }
try scoped(cStrings + [nil])
cStrings.forEach { free($0) }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment