Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a CGRect to an NSValue in an Array and vice versa

I have an array, full of CGRects. That part is fine. The problem is when I go to retrieve the CGRects from the array, I'm getting weird errors. Check out my code.

NSArray *frameLocations = [NSArray arrayWithObjects:
                           [NSValue valueWithCGRect:CGRectMake(20, 20, 121, 124)],
                           [NSValue valueWithCGRect:CGRectMake(176, 20, 121, 124)],
                           nil];

Than I get the frame in a for loop, like this:

 CGRect *imageFrame = [[frameLocations objectAtIndex:i] CGRectValue];

I've tried a bunch of different variations, spreading that line out over multiple variables. But I can't seem to get it.

like image 362
Andrew Avatar asked Jul 04 '11 18:07

Andrew


1 Answers

Try:

CGRect imageFrame = [[frameLocations objectAtIndex:i] CGRectValue];

You get an actual CGRect back, not a pointer to a CGRect, because it's a primitive C-style type, not an Objective-C object.

Apart from that your code looks correct.

like image 165
Tommy Avatar answered Oct 29 '22 03:10

Tommy