Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
692 views
in Technique[技术] by (71.8m points)

c - Converting an UnsafePointer with length to a Swift Array type

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...