Accessing sprites in a NSMutableArray returns an error - simple question
Why doesn't this work?
// spriteArray is an NSMutableArray
int spriteWidth = [spriteArray objectAtIndex:0].contentSize.width;
I get the error:
Request for memeber 'contentSize' in something not a structure or union
If I change the code:
CCSprite *tempSprite = [spriteArray objectAtIndex:0];
int spriteWidth = tempSprite.contentSize.width;
Then it's okay.
I've tried casting:
int spriteWidth = (CCSprite*)[spriteArray objectAtInde开发者_开发知识库x:0].contentSize.width;
But it doesn't work either.
Is there a way to do this without creating the sprite reference?
The return type of objectAtIndex:
is id
, which is not a struct
nor union
. If you want to use casting, try
((CCSprite*)[spriteArray objectAtIndex:0]).contentSize.width
Otherwise, use a temporary variable.
I believe the .
binds tighter than casting. Try
int spriteWidth = ((CCSprite*)[spriteArray objectAtIndex:0]).contentSize.width;
See this table.
精彩评论