Skip to content

Instantly share code, notes, and snippets.

@mvnrc
Last active February 18, 2020 03:40
Show Gist options
  • Save mvnrc/4373e6ab17f632623e1b98c93e49f9a3 to your computer and use it in GitHub Desktop.
Save mvnrc/4373e6ab17f632623e1b98c93e49f9a3 to your computer and use it in GitHub Desktop.
An extension of UnsafeMutablePointer for converting C array to Swift array
extension UnsafeMutablePointer {
/// Converts `C` decayed array to `Swift` array
///
/// - Parameter count: Number of elements in the `C` array
/// - Returns: Converted `Swift` array
public func toArray(count: Int) -> [Pointee] {
defer { self.deallocate() }
return Array(UnsafeBufferPointer(start: self, count: count))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment