Array float * in NSArray, iOS

I have an array of float pointers and I would like to convert it to NSArray.

Is there a better way to do this than iterating over float * and adding each record to NSArray?

I have:

float* data = new float[elements];
fill up data from binary ifstream

I want to not do something like:

NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:elements];
for (int i=0;i<elements;i++)
{
 [mutableArray addObject:[NSNumber numberWithFloat:data[i]]];
}
NSArray *array = [NSArray arrayWithArray:array];

Is there any convenient / more efficient way to copy a large piece of floats into NSArray?

Hi,

Owen

+3
source share
1 answer

You have two problems: firstly, you cannot store floatin NSArray, since it NSArraywill contain only Objective-C objects. You will then need to wrap the object, perhaps, NSNumberor NSValue.

, , . Id for:

for (int i = 0; i < elements; i++) {
    NSNumber *number = [NSNumber numberWithFloat:floatArray[i]];
    [myArray addObject:number];
}

, number . , - , :

for (int i = 0; i < elements; i++) {
    NSNumber *number = [[NSNumber alloc] initWithFloat:floatArray[i]];
    [myArray addObject:number];
    [number release];
}
+5

Source: https://habr.com/ru/post/1789785/


All Articles