为什么当我试图在函数中返回这个数组时,会得到错误“数组初始化器必须是一个初始化器列表”?

时间:2021-05-17 22:48:44

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 XCode http://i43.tinypic.com/35nal3n.png

我来自Java,和我很新的目标c。无论如何,我有这个静态方法旨在复制数组(如果有更好的方法来实现这一点,请让我知道,但我问这个问题更找出为什么我有这个错误以及如何避免这样的错误在未来。)我遇到了一些问题,但就在我认为所有问题都已解决的时候,我得到了这个看起来像XCode http://i43.tinypic.com/35nal3n.png的错误

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;
}

5 个解决方案

#1


4  

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

方法/函数不能返回C数组。你应该这样做

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

#2


6  

If all you really want is to copy the first n elements from one C array into another already existing array, probably the best way is to simply use memcpy:

如果你真正想要的是将前n个元素从一个C数组复制到另一个已经存在的数组中,那么最好的方法就是使用memcpy:

memcpy(targetArray, sourceArray, sizeof(sourceArray[0]) * numElements);

The sizeof(sourceArray[0]) calculates the byte-size of the type in your array (in your case, it's equivalent to sizeof(float).

sizeof(sourceArray[0])计算数组中类型的字节大小(在您的例子中,它等同于sizeof(float)。

#3


2  

C arrays are way more tricky than Java arrays. One of the biggest issues is that in a lot of instances, you don't know how large a C array is unless you have saved this information in a different variable, for example. The C FAQ "Arrays and Pointers" lists a lot of traps and they apply to Objective-C as well. You might want to see question 6.5 in particular.

C数组比Java数组要复杂得多。最大的问题之一是,在许多实例中,您不知道C数组有多大,除非您将这些信息保存在另一个变量中,例如。C FAQ“数组和指针”列出了许多陷阱,它们也适用于Objective-C。你可能特别想看问题6.5。

As @lwxted already suggested, try to avoid C arrays unless you really know what you're doing and you have determined that you need them. C arrays are faster than NSArray but unless you have determined that your array really is a performance bottleneck by measuring with a profiler you will most likely not notice any difference.

正如@lwxted已经建议的,尽量避免使用C数组,除非您确实知道自己在做什么,并且确定需要它们。C数组比NSArray要快,但是除非您通过使用分析器进行测量,确定您的数组确实是性能瓶颈,否则您很可能不会注意到任何差异。

And I strongly recommend avoiding a C array of Objective-C objects (id objects[]) unless you really, really know very well what you are doing (memory management issues).

我强烈建议避免使用Objective-C对象的C数组(id对象[]),除非您真的非常了解自己在做什么(内存管理问题)。

#4


1  

In Objective-C, unless for particular needs, a better way to handle this usually is to use the NSArray as opposed to C arrays.

在Objective-C中,除非有特殊需要,通常更好的处理方法是使用NSArray而不是C数组。

[NSArray arrayWithArray: array];

will copy an array.

将复制数组。

Besides, in this case, if you insist on using C arrays, the use of implicitly typed length float[] is advised against. A better way is to use pointers to manipulate arrays.

此外,在这种情况下,如果您坚持使用C数组,建议不要使用隐式类型长度float[]。更好的方法是使用指针来操作数组。

Also, the stack-allocated array would be invalid after leaving the function, since it's local only in the scope of the copyArray function. You should dynamically allocate memory, if you wish the array to be valid outside the scope.

此外,在离开函数后,堆栈分配的数组将无效,因为它仅在copyArray函数的范围内。如果希望数组在范围之外有效,则应该动态分配内存。

#5


1  

While I agree with all the points @DarkDust makes, if you're working with a C API such as OpenGL, there may be situations where using NSArray and NSNumber vs. C arrays of type float will have performance impacts. As always, try to use the simpler approach first, and carefully measure performance before deciding to optimize.

虽然我同意@DarkDust的所有观点,但是如果您使用像OpenGL这样的C API,那么在某些情况下,使用NSArray和NSNumber与float类型的C数组会对性能产生影响。和往常一样,首先尝试使用更简单的方法,在决定优化之前仔细地测量性能。

In any case, to answer the original question, here's how to correctly return a copy of a C array:

在任何情况下,要回答原始问题,以下是如何正确返回C数组的副本:

+ (float *)copyOfCArray:(float *)array withLength:(int)length
{
    float *copyOfArray = malloc(length * sizeof(float));

    for (int i = 0; i < length; i++) {
        copyOfArray[i] = array[i];
    }

    return copyOfArray;
}

Also, there's arguably no need to make the above a method at all. Instead, consider writing it as a C function:

此外,毫无疑问,完全没有必要将上述方法作为一种方法。相反,考虑把它写成C函数:

float *CopyArray(float *array, int length)
{
    // Implementation would be the same...
}

#1


4  

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

方法/函数不能返回C数组。你应该这样做

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

#2


6  

If all you really want is to copy the first n elements from one C array into another already existing array, probably the best way is to simply use memcpy:

如果你真正想要的是将前n个元素从一个C数组复制到另一个已经存在的数组中,那么最好的方法就是使用memcpy:

memcpy(targetArray, sourceArray, sizeof(sourceArray[0]) * numElements);

The sizeof(sourceArray[0]) calculates the byte-size of the type in your array (in your case, it's equivalent to sizeof(float).

sizeof(sourceArray[0])计算数组中类型的字节大小(在您的例子中,它等同于sizeof(float)。

#3


2  

C arrays are way more tricky than Java arrays. One of the biggest issues is that in a lot of instances, you don't know how large a C array is unless you have saved this information in a different variable, for example. The C FAQ "Arrays and Pointers" lists a lot of traps and they apply to Objective-C as well. You might want to see question 6.5 in particular.

C数组比Java数组要复杂得多。最大的问题之一是,在许多实例中,您不知道C数组有多大,除非您将这些信息保存在另一个变量中,例如。C FAQ“数组和指针”列出了许多陷阱,它们也适用于Objective-C。你可能特别想看问题6.5。

As @lwxted already suggested, try to avoid C arrays unless you really know what you're doing and you have determined that you need them. C arrays are faster than NSArray but unless you have determined that your array really is a performance bottleneck by measuring with a profiler you will most likely not notice any difference.

正如@lwxted已经建议的,尽量避免使用C数组,除非您确实知道自己在做什么,并且确定需要它们。C数组比NSArray要快,但是除非您通过使用分析器进行测量,确定您的数组确实是性能瓶颈,否则您很可能不会注意到任何差异。

And I strongly recommend avoiding a C array of Objective-C objects (id objects[]) unless you really, really know very well what you are doing (memory management issues).

我强烈建议避免使用Objective-C对象的C数组(id对象[]),除非您真的非常了解自己在做什么(内存管理问题)。

#4


1  

In Objective-C, unless for particular needs, a better way to handle this usually is to use the NSArray as opposed to C arrays.

在Objective-C中,除非有特殊需要,通常更好的处理方法是使用NSArray而不是C数组。

[NSArray arrayWithArray: array];

will copy an array.

将复制数组。

Besides, in this case, if you insist on using C arrays, the use of implicitly typed length float[] is advised against. A better way is to use pointers to manipulate arrays.

此外,在这种情况下,如果您坚持使用C数组,建议不要使用隐式类型长度float[]。更好的方法是使用指针来操作数组。

Also, the stack-allocated array would be invalid after leaving the function, since it's local only in the scope of the copyArray function. You should dynamically allocate memory, if you wish the array to be valid outside the scope.

此外,在离开函数后,堆栈分配的数组将无效,因为它仅在copyArray函数的范围内。如果希望数组在范围之外有效,则应该动态分配内存。

#5


1  

While I agree with all the points @DarkDust makes, if you're working with a C API such as OpenGL, there may be situations where using NSArray and NSNumber vs. C arrays of type float will have performance impacts. As always, try to use the simpler approach first, and carefully measure performance before deciding to optimize.

虽然我同意@DarkDust的所有观点,但是如果您使用像OpenGL这样的C API,那么在某些情况下,使用NSArray和NSNumber与float类型的C数组会对性能产生影响。和往常一样,首先尝试使用更简单的方法,在决定优化之前仔细地测量性能。

In any case, to answer the original question, here's how to correctly return a copy of a C array:

在任何情况下,要回答原始问题,以下是如何正确返回C数组的副本:

+ (float *)copyOfCArray:(float *)array withLength:(int)length
{
    float *copyOfArray = malloc(length * sizeof(float));

    for (int i = 0; i < length; i++) {
        copyOfArray[i] = array[i];
    }

    return copyOfArray;
}

Also, there's arguably no need to make the above a method at all. Instead, consider writing it as a C function:

此外,毫无疑问,完全没有必要将上述方法作为一种方法。相反,考虑把它写成C函数:

float *CopyArray(float *array, int length)
{
    // Implementation would be the same...
}