2009-08-18 68 views
0

我试图让一个数组中的对象等于另一个数组。如果我单独设置每个属性,它可以正常工作,但我想将一个对象设置为等于antoher而不写入函数以遍历每个属性。ObjectiveC将对象从一个NSMutableArray复制到另一个“无效的左值赋值”

-(void)DrawCard: (int) wp{ 
[self GetNc :(wp)]; 
if (nc > 0){  
    ((card*) [[Hands objectAtIndex:wp] objectAtIndex:nc]) = ((card*) [[Draws objectAtIndex:wp] objectAtIndex:0]) 
} 

}

(WP代表该玩家。所有GetNc做它设定的整数NC(新卡),通过查找当前在手使用的卡对象指数最高)。

这不会编译,但设置一个单独的属性确实:

((card*) [[Hands objectAtIndex:wp] objectAtIndex:nc]).suit = ((card*) [[Draws objectAtIndex:wp] objectAtIndex:0]).suit; 

,并试图不给铸造无效L值误差以及即:

 [[Hands objectAtIndex:wp] objectAtIndex:nc] = [[Draws objectAtIndex:wp] objectAtIndex:0]; 

(有没有可能设置一个数组对象而不需要强制转换?)

我很感谢您的时间阅读和回复。干杯!

回答

2

这会将数组的索引设置为指向同一个对象。我希望你明白,这不会复制对象。

[[Hands objectAtIndex:wp] replaceObjectAtIndex:nc withObject:[[Draws objectAtIndex:wp] objectAtIndex:0]]; 
+0

因此,如果Draws中的对象发生变化,Hands和Draws会返回新的Draws值?谢谢..我如何复制对象,以便我可以独立调整它们? – user128526 2009-08-18 18:18:51

+0

如果对象支持复制,则可以使用“复制”方法复制它。 – newacct 2009-08-19 05:59:51

+0

你能举个例子吗?有问题的对象是我写的卡片类。我如何让他们支持复制? – user128526 2009-08-28 18:52:50

相关问题