How to store CGRect values in NSMutableArray?

Krishna Chaitanya Bandaru picture Krishna Chaitanya Bandaru · May 29, 2010 · Viewed 25.3k times · Source

How would I store CGRect objects in a NSMutableArray, and then later retrieve them?

Answer

Jason Coco picture Jason Coco · May 29, 2010

You need to wrap the CG structures in NSValue classes. So:

NSMutableArray* array = [NSMutableArray mutableArray];
[array addObject:[NSValue valueWithCGRect:CGRectMake(0,0,10,10)]];
CGRect someRect = [[array objectAtIndex:0] CGRectValue];