如何将CGRect对象存储在NSMutableArray中,然后再检索它们?
答案 0 :(得分:286)
您需要将CG结构包装在NSValue
类中。所以:
NSMutableArray* array = [NSMutableArray mutableArray];
[array addObject:[NSValue valueWithCGRect:CGRectMake(0,0,10,10)]];
CGRect someRect = [[array objectAtIndex:0] CGRectValue];
答案 1 :(得分:12)
CGRect rect = CGRectMake( 5, 5, 40, 30 );
NSString* rectAsString = NSStringFromCGRect( rect );
CGRect original = CGRectFromString( rectAsString );
您如何看待这种存储CGRect日期的方式?
答案 2 :(得分:2)
我们将CGRect
,CGPoint
,CMTime
个对象存储在NSMutableArray
中,
[arrayName addObject:[NSValue valueWithCGPoint:MyCGPoint]]
[arrayName addObject:[NSValue valueWithCGRect:MyCGRect]]
[arrayName addObject:[NSValue valueWithCMTime:MyCMTime]]
[arrayName addObject:[NSValue valueWithCMTimeRange:MyCMTimeRange]]
答案 3 :(得分: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);