简体   繁体   English

分配数组引用,不兼容的类型?

[英]Assigning array references, incompatible types?

I have a custom class called ItemComponent. 我有一个名为ItemComponent的自定义类。 Another class has an array of them called subComponents. 另一个类具有一个称为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. 最初,将其设置为0,因为并非所有对象都具有子组件。

In the implementation, I have a method to add an ItemComponent to the property. 在实现中,我有一个向属性添加ItemComponent的方法。 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 . 在这种情况下,将组件保留在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. 如果您想无后顾之忧,请设置一个小型测试,该测试将对某些NSMutableArrays数千次迭代,甚至可能更改其中的一些。 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.) (根据您要执行的操作,0.005部分可能太陡了,但我想您已经明白了。要构建一个完整的帧,50 fps≈1/50 s = 0.02 s = 20 ms。如果整个Objective- C开销花费了10毫秒,其余时间大约还有20毫秒,并保持了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. 在某些情况下,您可能想使用纯C数组,例如,当您编写粒子引擎并且数组中有成千上万个项目会改变每个循环时。 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. 按照定义的方式,subComponents数组实际上将其(零)项存储在其他类变量中。 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. 为此,您需要使其成为一个指向ItemComponent的指针,而不是一个零长度的ItemComponents数组。

暂无
暂无

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

相关问题 从'nsarray'分配给'nsmutablearray'的不兼容指针类型 - incompatible pointer types assigning to 'nsmutablearray ' from 'nsarray ' 分配给&#39;NSString&#39;到&#39;NSString&#39;的不兼容指针类型 - Incompatible pointer types assigning to 'NSArray' to 'NSString' “不兼容的 objective-c 类型分配”错误 - 'Incompatible objective-c types assigning' error 从nsdictionary分配给nsarray的不兼容指针类型 - incompatible pointer types assigning to nsarray from nsdictionary 分配给NSObject的指针类型不兼容 <MGTwitterEngineDelegate> * __来自NSObject的弱点 - Incompatible pointer types assigning to NSObject<MGTwitterEngineDelegate> *__weak from NSObject 分配给“ uiviewcontroller”的不兼容的指针类型 <hellodelegate> “来自uinavigationcontroller - incompatible pointer types assigning to “uiviewcontroller<hellodelegate>” from uinavigationcontroller 不兼容的指针类型分配给&#39;UILabel * __ weak&#39;到&#39;NSString * __ strong&#39;? - incompatible pointer types assigning to 'UILabel *__weak' to 'NSString *__strong'? Obj-C:不兼容的指针类型从&#39;NSNumber * _strong分配给&#39;UITextfield * _strong&#39; - Obj-C: Incompatible pointer types assigning to 'UITextfield *_strong' from 'NSNumber *_strong 分配给&#39;ViewController&#39;的指针类型不兼容 - Incompatible pointer type assigning to 'ViewController' 从不兼容的类型警告中分配 - Assigning to from incompatible type warning
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM