简体   繁体   中英

Assigning array references, incompatible types?

I have a custom class called ItemComponent. Another class has an array of them called subComponents. It's a property of the class:

ItemComponent *subComponents[0];

Initially, it is set as 0, because not all objects will have sub-components.

In the implementation, I have a method to add an ItemComponent to the property. The item is passed in, and the assignment is coded like this (after necessary checks & switching):

ItemComponent *tempArray[1];
tempArray[0] = item;
subComponents = tempArray;

I get the error: 'incompatible types in assignment' on the last line.

They are both pointers to arrays, so what gives?

Thanks!

First, as I already wrote in a comment, I think you are sweating it too much. I guess we are talking about some game object that can have some components in it. In this case it is perfectly affordable to keep the components in an NSArray . The code will be easier to work with and the speed difference will be small.

If you want to sleep without worries, set up a small test that will iterate over some NSMutableArrays several thousand times and maybe even change some of them. Then measure the time it takes:

double start = CFAbsoluteTimeGetCurrent();
// now iterate over the arrays, send some messages, change contents
double end = CFAbsoluteTimeGetCurrent();
double diff = end-start; // in seconds
NSAssert(diff < 0.005, @"I’ll be damned.");

(The 0.005 part might be too steep depending on what you are trying to do, but I guess you get the point. 50 fps ≈ 1/50 s = 0.02 s = 20 ms for a whole frame to build. If the whole Objective-C overhead took 10 ms, you'd still have about 20 ms for the rest and keep a decent framerate of 30 fps.)

There are cases where you would want to use plain C array, for example when you're writing a particle engine and have several thousands of items in an array that changes every loop. In that case you can simply use a pointer:

ItemComponent *components;
int compMax;
// …
compMax = 1000;
components = calloc(compMax, sizeof(ItemComponent));
NSAssert(components != NULL, @"Out of memory for components.");
components[0] = …;
components[1] = …;
// …
free(components);

I am hundred percent sure about the first point. I am not that much sure about the second, maybe there's a safer way to do it, but I would write it this way.

您使用C数组而不是NSMutableArrays是否有原因?

Try this to see if it makes a difference.

ItemComponent *subComponents[];

The way you have it defined, the subComponents array actually stores its (zero) item right there among your other class variables. You can't reset it to point elsewhere. In order to do that, you need to make it a pointer to a pointer to ItemComponent, instead of an array of zero length of ItemComponents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM