Converting an UnsafePointer with length to a Swift Array type

Ephemera

I'm looking for the simplest ways to achieve reasonable C interoperability in Swift, and my current block is converting an UnsafePointer<Int8> (which was a const char *), into an [Int8] array.

Currently, I have a naïve algorithm that can take an UnsafePointer and a number of bytes and converts it to an array, element by element:

func convert(length: Int, data: UnsafePointer<Int8>) {

    let buffer = UnsafeBufferPointer(start: data, count: length);
    var arr: [Int8] = [Int8]()
    for (var i = 0; i < length; i++) {
        arr.append(buffer[i])
    }
}

The loop itself can be sped up by using arr.reserveCapacity(length), however that does not remove the issue of the loop itself.

I'm aware of this SO question which covers how to convert UnsafePointer<Int8>to String, however String is a different beast entirely to [T]. Is there a convenient Swift way of copying length bytes from an UnsafePointer<T> into a [T]? I'd prefer pure Swift methods, without passing through NSData or similar. If the above algorithm is really the only way to do it, I'm happy to stick with that.

Martin R

You can simply initialize a Swift Array from an UnsafeBufferPointer:

func convert(length: Int, data: UnsafePointer<Int8>) -> [Int8] {

    let buffer = UnsafeBufferPointer(start: data, count: length);
    return Array(buffer)
}

This creates an array of the needed size and copies the data.

Or as a generic function:

func convert<T>(count: Int, data: UnsafePointer<T>) -> [T] {

    let buffer = UnsafeBufferPointer(start: data, count: count);
    return Array(buffer) 
}

where length is the number of items that the pointer points to.

If you have a UInt8 pointer but want to create an [T] array from the pointed-to data, then this is a possible solution:

// Swift 2:
func convert<T>(length: Int, data: UnsafePointer<UInt8>, _: T.Type) -> [T] {

    let buffer = UnsafeBufferPointer<T>(start: UnsafePointer(data), count: length/strideof(T));
    return Array(buffer) 
}

// Swift 3:
func convert<T>(length: Int, data: UnsafePointer<UInt8>, _: T.Type) -> [T] {
    let numItems = length/MemoryLayout<T>.stride
    let buffer = data.withMemoryRebound(to: T.self, capacity: numItems) {
        UnsafeBufferPointer(start: $0, count: numItems)
    }
    return Array(buffer) 
}

where length now is the number of bytes. Example:

let arr  = convert(12, data: ptr, Float.self)

would create an array of 3 Floats from the 12 bytes pointed to by ptr.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Swift - converting from UnsafePointer<UInt8> with length to String

From Dev

converting ObjC to Swift - UnsafePointer<Void> not convertible to struct type

From Dev

converting ObjC to Swift - UnsafePointer<Void> not convertible to struct type

From Dev

From UnsafePointer<UnsafePointer<CFloat>> to an array of floats in Swift?

From Dev

Array could be potentially converted to UnsafePointer in Swift, why?

From Dev

How does NSData(bytes:length:) convert [Byte] to UnsafePointer<Void> in Swift?

From Dev

Converting a type to an array

From Dev

how to pass a swift Array as UnsafePointer<T> argument in a function

From Dev

UnsafePointer in Swift 3

From Dev

converting char array of particular length into a string

From Dev

converting array of variable length to tuple in scala

From Dev

String length when converting from a character array

From Dev

String length when converting from a character array

From Dev

Swift converting Byte Array into String

From Dev

Converting JSON to array in Swift 2

From Dev

Swift converting Byte Array into String

From Dev

Swift: cannot convert value of type 'Self' to expected argument type 'UnsafePointer<Void>'

From Dev

What data type should I use when converting a byte array from Swift to Objective-C

From Dev

For loop based on array length in Swift

From Dev

Swift access to variable length array

From Dev

Converting String array to CGFloat array in Swift

From Dev

Converting array of C strings to Swift string array

From Dev

How Does Parameter Type (void *) In Objective-C Translate To UnsafePointer<()> In Swift?

From Dev

Cannot convert value of type 'UnsafePointer<Double>' to expected argument type 'UnsafePointer<_>'

From Dev

How to correctly initialize an UnsafePointer in Swift?

From Dev

Using CFArrayGetValueAtIndex in Swift with UnsafePointer (AUPreset)

From Dev

Swift UnsafeMutablePointer & UnsafeMutablePointer<UnsafePointer<SomeType>>

From Dev

How to correctly initialize an UnsafePointer in Swift?

From Dev

Swift UnsafeMutablePointer & UnsafeMutablePointer<UnsafePointer<SomeType>>

Related Related

  1. 1

    Swift - converting from UnsafePointer<UInt8> with length to String

  2. 2

    converting ObjC to Swift - UnsafePointer<Void> not convertible to struct type

  3. 3

    converting ObjC to Swift - UnsafePointer<Void> not convertible to struct type

  4. 4

    From UnsafePointer<UnsafePointer<CFloat>> to an array of floats in Swift?

  5. 5

    Array could be potentially converted to UnsafePointer in Swift, why?

  6. 6

    How does NSData(bytes:length:) convert [Byte] to UnsafePointer<Void> in Swift?

  7. 7

    Converting a type to an array

  8. 8

    how to pass a swift Array as UnsafePointer<T> argument in a function

  9. 9

    UnsafePointer in Swift 3

  10. 10

    converting char array of particular length into a string

  11. 11

    converting array of variable length to tuple in scala

  12. 12

    String length when converting from a character array

  13. 13

    String length when converting from a character array

  14. 14

    Swift converting Byte Array into String

  15. 15

    Converting JSON to array in Swift 2

  16. 16

    Swift converting Byte Array into String

  17. 17

    Swift: cannot convert value of type 'Self' to expected argument type 'UnsafePointer<Void>'

  18. 18

    What data type should I use when converting a byte array from Swift to Objective-C

  19. 19

    For loop based on array length in Swift

  20. 20

    Swift access to variable length array

  21. 21

    Converting String array to CGFloat array in Swift

  22. 22

    Converting array of C strings to Swift string array

  23. 23

    How Does Parameter Type (void *) In Objective-C Translate To UnsafePointer<()> In Swift?

  24. 24

    Cannot convert value of type 'UnsafePointer<Double>' to expected argument type 'UnsafePointer<_>'

  25. 25

    How to correctly initialize an UnsafePointer in Swift?

  26. 26

    Using CFArrayGetValueAtIndex in Swift with UnsafePointer (AUPreset)

  27. 27

    Swift UnsafeMutablePointer & UnsafeMutablePointer<UnsafePointer<SomeType>>

  28. 28

    How to correctly initialize an UnsafePointer in Swift?

  29. 29

    Swift UnsafeMutablePointer & UnsafeMutablePointer<UnsafePointer<SomeType>>

HotTag

Archive