简体   繁体   English

当我试图在函数中返回此数组时,为什么会出现错误“数组初始值设定项必须是初始化列表”?

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

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很新。无论如何,我有这个静态方法,它旨在制作一个数组的副本 (如果有更好的方法来实现这一点,请告诉我,但我我更多地问这个问题,以便找出我为什么会遇到这个错误以及如何在将来避免这样的错误。)我遇到了一些问题,但就在我认为我把它们全部整理好的时候,我得到了这个错误看起来像 的XCode

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

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)

method/function cannot return C array. 方法/函数不能返回C数组。 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];
    }
}

C arrays are way more tricky than Java arrays. C数组比Java数组更棘手。 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. 其中一个最大的问题是,在很多情况下,除非您将此信息保存在不同的变量中,否则您不知道C数组的大小。 The C FAQ "Arrays and Pointers" lists a lot of traps and they apply to Objective-C as well. C FAQ“Arrays and Pointers”列出了很多陷阱,它们也适用于Objective-C。 You might want to see question 6.5 in particular. 您可能希望特别看到问题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. 正如@lwxted已经建议的那样,尽量避免使用C数组,除非你真的知道自己在做什么并确定自己需要它们。 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. 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 objects[] ),除非你真的非常清楚你在做什么(内存管理问题)。

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. 此外,在这种情况下,如果你坚持使用C数组,建议使用隐式类型的长度float[] A better way is to use pointers to manipulate arrays. 更好的方法是使用指针来操作数组。

Also, the stack-allocated array would be invalid after leaving the function, since it's local only in the scope of the copyArray function. 此外,堆栈分配的数组在离开函数后将无效,因为它仅在copyArray函数的范围内是copyArray You should dynamically allocate memory, if you wish the array to be valid outside the scope. 如果希望数组在范围之外有效,则应动态分配内存。

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. 虽然我同意@DarkDust提出的所有要点,但如果您正在使用OpenGL等C API,可能会出现使用NSArrayNSNumberfloat类型的C数组的情况会产生性能影响的情况。 As always, try to use the simpler approach first, and carefully measure performance before deciding to optimize. 与往常一样,首先尝试使用更简单的方法,并在决定优化之前仔细测量性能。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 声明数组时“初始化元素不是恒定的” - “initializer element is not constant” when declaring an array 为什么尝试返回从Parse.com提取的UIImage时出现此错误? - Why do I get this error when trying to return a UIImage fetched from Parse.com? 为什么在尝试使用presentViewController时出现此错误? - Why do I get this error when trying to use presentViewController? 我不了解类的Objective-C初始化程序。 - I do not understand Objective-C Initializer for a class. 为什么在尝试编译此代码时会出现错误“error:unknown type name&#39;virtual&#39;”? - Why do I get the error “error: unknown type name 'virtual'” when trying to compile this code? char数组的初始化字符串太长 - Initializer-string for char array is too long Swift:我需要为函数返回创建一个新的不可变数组吗? - Swift: do I need to create a new immutable array for function return? 为什么在尝试将addObject添加到NSMutableArray时会收到SIGABRT - Why do I get SIGABRT when trying to addObject to NSMutableArray 创建新的指定初始值设定项时,是否必须覆盖init? - Must you override init when you create a new designated initializer? 为什么只在调试时才获取值,而不执行? - Why do I only get values when I'm debugging, not executing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM