You can convert a swift::Array to a std::vector using the following explicit constructor of std::vector:
auto cxxVector = std::vector<int>(intArray.begin(), intArray.end());
This constructor copies over the elements from the Swift array into the constructed C++ vector.
You can also convert a vector to a swift::Array using the following explicit constructor of swift::Array:
auto swiftIntArray = swift::Array<int>(cxxVector);
This constructor copies over the elements from the C++ vector into the constructed Swift array.
I have a type in cpp that takes a vector.I am not able to figure out how to construct this vector instance from swift.
It seems that in cpp there is an easy way to convert back and forth for these, I am guessing that this suggests that I should add a contractor that takes a swift array.
I would like to take a swift array and convert it to a vector that I can just pass in to the constructor.
This is how the std::vector init looks like in swift
let someInstance = scad.KeyGrid.init(data: std.__1.vector<vector<UnsafeMutablePointer<Key>, allocator<UnsafeMutablePointer<Key>>>, allocator<vector<UnsafeMutablePointer<Key>, allocator<UnsafeMutablePointer<Key>>>>>)
C++ templates cannot be instantiated from Swift code, AIUI. I believe you have to typedef std::vector<...> MyVector; or using MyVector = std::vector<...>; in C++ somewhere, and then use the MyVector alias in Swift.
// swift side
func convert (_ inputArray: [scad.Key]) -> scad.KeyVector {
var tempResult = scad.KeyVector()
tempResult.reserve(inputArray.count)
for item in inputArray {
tempResult.push_back(item) // cannot convert value of type 'scad.Key' to expected argument type 'std.__1.vector<UnsafeMutablePointer<Key>, allocator<UnsafeMutablePointer<Key>>>.value_type?' (aka 'Optional<UnsafeMutablePointer<scad.Key>>')sourcek
}
return tempResult
}
Using a vector of UnsafeMutablePointers from Swift is usually not a good idea, because it's quite tricky to do it in a memory-safe way. I would recommend using a std::vector<Key> if possible, then you will be able to simply use scad.KeyVector(inputArray) to convert a Swift array to a vector. Would this be viable for your use case?