Creating a NSArray from a C Array

℡╲_俬逩灬. 提交于 2019-12-10 09:30:46

问题


There are many threads about going the opposite way, but I am interested in converting from a primitive C array to a NSArray. The reason for this is that I want to create a NSString from the array contents. To create the NSString I will use:

NSArray *array;
NSString *stringFromArray = [array componentsJoinedByString:@","];

I am joining the elements of the array by commas because I will later be saving the string as a .csv file. I don't think it matters, but the C array I am dealing with is of type double and size 43.

double c_array = new double [43];

Thanks!


回答1:


NSString * stringFromArray = NULL;
NSMutableArray * array = [[NSMutableArray alloc] initWithCapacity: 43];
if(array)
{
    NSInteger count = 0;

    while( count++ < 43 )
    {
        [array addObject: [NSString stringWithFormat: @"%f", c_array[count]]];
    }

    stringFromArray = [array componentsJoinedByString:@","];
    [array release];     
}


来源:https://stackoverflow.com/questions/8086634/creating-a-nsarray-from-a-c-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!