113 votes

Comment stocker les valeurs de CGRect dans NSMutableArray ?

Comment puis-je stocker des objets CGRect dans un NSMutableArray, puis les récupérer ultérieurement ?

291voto

Jason Coco Points 52303

Vous devez envelopper les structures CG dans NSValue classes. Donc :

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

0 votes

Plus d'exemples ici : iosdevelopertips.com/cocoa/

0 votes

Excellente réponse ! Ce serait encore plus utile si vous démontriez le "déblocage" de la valeur NS stockée en CGRect.

0 votes

Non disponible sur MacOS

13voto

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

2voto

Vaibhav Sharma Points 300

Nous stockons les CGRect , CGPoint , CMTime dans un NSMutableArray ,

[arrayName addObject:[NSValue valueWithCGPoint:MyCGPoint]]

[arrayName addObject:[NSValue valueWithCGRect:MyCGRect]]

[arrayName addObject:[NSValue valueWithCMTime:MyCMTime]]

[arrayName addObject:[NSValue valueWithCMTimeRange:MyCMTimeRange]]

1voto

Tanvi Jain Points 423
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);

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X