Getting a multidimensional array from C api, how to handle in Swift?

I am using the C library in my Swift application, and I cannot figure out how to get a multidimensional array that the C method should return.

I get this from the C API:

struct resultArray
{
    double *data;
    int size[2];
};

Where:

size = matrix size, an array of two elements with the number of rows and the number of columns

data = matrix data

In swift, I can do the following to get the size:

let numRows = Int(results.size.0)
let numColoumns = Int(results.size.1)

But I don’t understand how to get the matrix so that I can iterate over it? I tried the following:

let matrixResult = results.data.memory

This seems to only return a double value, because matrixResult becomes Double.

Then I tried this:

let matrixResult = results.data

What matrixResult an does:

UnsafeMutablePointer<Double>

, C. Swift... - , ?

+4
2

C. C. , data , , Swift

let firstRow = Array(UnsafeBufferPointer(start: results.data, count: numColumns))

"" Swift

let matrix = (0 ..< numRows).map { row in
    Array(UnsafeBufferPointer(start: results.data + numColumns * row, count: numColumns))
}

( , Melifaro ), , :

let matrix = (0 ..< numRows).map { row in
    UnsafeBufferPointer(start: results.data + numColumns * row, count: numColumns)
}

 let element = matrix[row][col]

. , , matrix, by Swift.

+3

BTW. , C-Structure, Swift. , :

let (I, J) = res.size

for i in 0..<I {
    for j in 0..<J {
        let v = res.data.advancedBy(Int(j + i * J))
        let d: Double = v.memory
        print("[\(i), \(j)]: \(d)")
    }
}
+3

All Articles