xcode 当我尝试在函数中返回此数组时,为什么会收到错误“数组初始值设定项必须是初始值设定项列表”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20939339/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do I get the error "Array initializer must be an initializer list" when I'm trying to return this array in a function?
提问by michaelsnowden
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
我来自 Java,我对 Objective C 很陌生。无论如何,我有这个静态方法,它旨在制作一个数组的副本(如果有更好的方法来完成这个,请告诉我,但我我问这个问题更多 - 所以要找出我为什么会出现这个错误以及如何避免将来出现这样的错误。)我遇到了一些问题,但是就在我以为我已经把它们全部解决了的时候,我得到了这个错误看起来像
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;
}
采纳答案by Bryan Chen
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];
}
}
回答by DarkDust
If all you really want is to copy the first nelements from one C array into another already existingarray, probably the best way is to simply use memcpy
:
如果您真正想要的是将一个 C 数组中的前n 个元素复制到另一个已经存在的数组中,那么最好的方法可能是简单地使用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)
。
回答by DarkDust
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 数组有多大。该ç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 arefaster than NSArray
but unless you have determined that your array really is a performance bottleneck by measuring with a profileryou 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, reallyknow very well what you are doing (memory management issues).
我强烈建议避免使用 Objective-C 对象 ( id objects[]
)的 C 数组,除非您真的非常清楚自己在做什么(内存管理问题)。
回答by lwxted
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
.
将复制一个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
函数范围内是本地的。如果您希望数组在作用域外有效,您应该动态分配内存。
回答by jlehr
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 提出的所有观点,但如果您使用的是 C API,例如 OpenGL,则可能会出现使用NSArray
和NSNumber
与 C 类型的数组float
会对性能产生影响的情况。与往常一样,首先尝试使用更简单的方法,并在决定优化之前仔细衡量性能。
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...
}