¿Cómo puedo agregar objetos CGPoint a un NSArray de la manera más fácil?


Tengo alrededor de 50 objetos CGPoint que describen algo así como una "ruta", y quiero agregarlos a un NSArray. Va a ser un método que sólo devolverá el CGPoint correspondiente para un índice dado. No quiero crear 50 variables como p1 = ...; p2=... y así sucesivamente. ¿Hay una manera fácil que me permita definir esos puntos "instantáneamente" al inicializar el NSArray con objetos?

Author: Thanks, 2009-05-22

4 answers

Con UIKit Apple agregó soporte para CGPoint a NSValue, por lo que puede hacer:

NSArray *points = [NSArray arrayWithObjects:
                     [NSValue valueWithCGPoint:CGPointMake(5.5, 6.6)],
                     [NSValue valueWithCGPoint:CGPointMake(7.7, 8.8)],
                     nil];

Enumere tantas instancias [NSValue] como tenga CGPoint, y termine la lista en nil. Todos los objetos de esta estructura se liberan automáticamente.

Por otro lado, cuando estás sacando los valores de la matriz:

NSValue *val = [points objectAtIndex:0];
CGPoint p = [val CGPointValue];
 318
Author: Jarret Hardie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-05-22 19:44:40

Yo uso esto:

Crear matriz:

NSArray *myArray = @[[NSValue valueWithCGPoint:CGPointMake(30.0, 150.0)],[NSValue valueWithCGPoint:CGPointMake(41.67, 145.19)]];

Obtener el 1er objeto CGPoint:

CGPoint myPoint = [myArray[0] CGPointValue];
 7
Author: Tibidabo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-06 02:22:31

También puede escribir esto en una forma mínima de:

CGPoint myArray[] = { CGPointMake(5.5, 6.6), CGPointMake(7.7, 8.8) };

CGPoint p2 = myArray[1];
 3
Author: GilesDMiddleton,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-09-16 11:36:19

¿has echado un vistazo a CFMutableArray? Eso podría funcionar mejor para ti.

 2
Author: Ramin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-05-22 19:44:58