I want to call a C++ function in Swift where I provide a vector and obtain another vector. In the objective-C part of the bridge, I want to know what would be the best way to convert the NSArray to std::vector, and the std::vector back to NSArray.
Right now, I am looping over the elements:
- For the input array to the std::vector that is needed for the c++ function:
- (NSMutableArray*) foo:(NSArray *)input {
std::vector<uint8_t> my_vector;
for (int j = 0; j< [input count]; ++j) {
uint8_t myInt = [input[j] intValue];
my_vector.push_back(myInt);
}
...
}
- For the conversion back from std::vector to the return of the function, which is an NSMutableArray, I am also doing a loop over each element:
- (NSMutableArray*) foo:(NSArray *)input {
...
std::vector<int16_t> my_output_vector = foo_cpp();
NSMutableArray * output = [NSMutableArray new];
for (int j = 0; j< my_output_vector.size(); ++j) {
[output addObject: @(my_output_vector[j])];
}
return output;
}
Is there a more elegant way to do this? Or since I am using uint8_t and int16_t this is the only options?
Cheers