convert std:vector to NSArray

N_A picture N_A · Apr 18, 2012 · Viewed 11.6k times · Source

Is there a good way to convert a vector<int32_t> to an NSArray of NSNumber or is looping and adding to an NSMutableArray pretty much the only way?

Answer

Richard J. Ross III picture Richard J. Ross III · Apr 18, 2012

If you have a vector of objects, you can do the following:

NSArray *myArray = [NSArray arrayWithObjects:&vector[0] count:vector.size()];

However, if your vector contains primitive types, such as NSInteger, int, float, etc., you would have to manually loop the values in the vector and convert them to a NSNumber first.