如何在NSMutableArray中存储CGRect值?


Answers:


289

您需要将CG结构包装在NSValue类中。所以:

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


好答案!如果您演示了将存储的NSValue“拆箱”回CGRect,则将更加有用。
Motti Shneor '19

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

您如何看待这种存储CGRect日期的方式?


2

我们存储CGRectCGPointCMTime在一个对象NSMutableArray

[arrayName addObject:[NSValue valueWithCGPoint:MyCGPoint]]

[arrayName addObject:[NSValue valueWithCGRect:MyCGRect]]

[arrayName addObject:[NSValue valueWithCMTime:MyCMTime]]

[arrayName addObject:[NSValue valueWithCMTimeRange:MyCMTimeRange]]


1
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);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.