How to store CGRect values in NSMutableArray?

IphoneNsmutablearrayCore Graphics

Iphone Problem Overview


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

Iphone Solutions


Solution 1 - Iphone

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];

Solution 2 - Iphone

CGRect rect = CGRectMake( 5, 5, 40, 30 );
NSString* rectAsString = NSStringFromCGRect( rect );
CGRect original = CGRectFromString( rectAsString );

Solution 3 - Iphone

We store the CGRect,CGPoint,CMTime objects in a NSMutableArray,

[arrayName addObject:[NSValue valueWithCGPoint:MyCGPoint]]

[arrayName addObject:[NSValue valueWithCGRect:MyCGRect]]

[arrayName addObject:[NSValue valueWithCMTime:MyCMTime]]

[arrayName addObject:[NSValue valueWithCMTimeRange:MyCMTimeRange]]

Solution 4 - Iphone

Store string in array.and then get back string and convert that in CGRect back as per the need.
CGRect rect = CGRectMake( 5, 5, 40, 30 );
NSString* rectAsString = NSStringFromCGRect( rect );
NSMutableArray* array = [NSMutableArray mutableArray];
[array addObject:rectAsString];

For converting string in CGRect back use:-
CGRect rect9 = CGRectFromString(rectAsString);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKrishna Chaitanya BandaruView Question on Stackoverflow
Solution 1 - IphoneJason CocoView Answer on Stackoverflow
Solution 2 - IphoneJaneView Answer on Stackoverflow
Solution 3 - IphoneVaibhav SharmaView Answer on Stackoverflow
Solution 4 - IphoneTanvi JainView Answer on Stackoverflow