codedeziner
codedeziner

Reputation: 137

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 objectAtIndex:0].contentSize.width;

But it doesn't work either.

Is there a way to do this without creating the sprite reference?

Upvotes: 0

Views: 419

Answers (2)

dreamlax
dreamlax

Reputation: 95335

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.

Upvotes: 2

David Kanarek
David Kanarek

Reputation: 12613

I believe the . binds tighter than casting. Try

int spriteWidth = ((CCSprite*)[spriteArray objectAtIndex:0]).contentSize.width;

See this table.

Upvotes: 2

Related Questions