user128526
user128526

Reputation: 111

ObjectiveC Copying object from one NSMutableArray to another "invalid lvalue assignment"

I'm trying to make an object in one array equal to another. If I set each property individually, it works fine, but I want to set one object equal to antoher without writing a function to run through each property.

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

}

(wp stands for which player. All GetNc does it set the integer nc (for new card), by finding the highest index of a card object currently being used in Hands).

That won't compile, but setting an individual property does:

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

And trying without casting gives invalid l-value error as well ie:

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

(Is it ever possible to set an array object without casting?)

I appreciate your time to read and respond. Cheers!

Upvotes: 0

Views: 1316

Answers (1)

newacct
newacct

Reputation: 122439

This will set that index of the array to point to the same object. I hope you understand that this does not copy the object.

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

Upvotes: 2

Related Questions