PDA

View Full Version : Array error




swiftd
Jul 27, 2009, 07:09 PM
Hey guys. Why is it that this will work:

tile1.image = [characterToTile characterImage];

but this doesn't? (please note I pass the value 1 as the position parameter):

NSArray *characterArray = [[NSArray alloc] initWithObjects: tile1, tile2, nil];
-(void)putCharacter: (Character)characterToTile onTile: (NSInteger) charPosition {
[characterArray objectAtPosition:charPosition].image = [characterToTile characterImage];

I don't have the compile error in front of me but it's the one about request for member in something not a structure or union. Any help would be greatly appreciated!!



moral-hazard
Jul 27, 2009, 08:54 PM
Try casting it. The compiler doesn't know what type of object to expect out of the array, so it can't possibly know that it will respond to your property getter (the ".image")

The cast would look like this:
((ClassName *)[characterArray objectAtPosition:charPosition]).image

Where "ClassName" is whatever class of object it is that is coming out of the array (i.e. the object at the position you are specifying)

swiftd
Jul 28, 2009, 11:56 PM
Thank you!!