Search code examples
iphonensarraycgrect

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.


Solution

  • 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.