Why do I get the error "Array initializer must be an initializer list" when I'm trying to return this array in a function?

arrays, initialization, objective-c, xcode

Solution

method/function cannot return C array. you should do this

+ (void) copyArrayFrom:(float *)array to:(float *)toArray withLength: (unsigned) length
{
    for (int i = 0; i < length; i++)
    {
        toArray [i] = array[i];
    }
}

Problem

I am coming from Java, and I'm very new to Objective C. Anyway, I have this static method which is designed to make a copy of an array (if there's a better way to accomplish this, please let me know, but I'm asking this question more-so to find out why I got this error and how to avoid such an error in the future.) I ran into some problems with it, but just when I thought I had them all sorted out, I got this error that looked like Here is the method in the interface: ``` + (float[]) copyArray: (float[]) array withLength: (int) length; ``` And here is the method in the implementation: ``` + (float[]) copyArray: (float[]) array withLength: (int) length { float copiedArray[length]; for (int i = 0; i < length; i++) { copiedArray[i] = array[i]; } return copiedArray; } ```

Original source

Related problems