How can I add CGPoint objects to an NSArray the easy way?

后端 未结 4 1211
情书的邮戳
情书的邮戳 2020-11-30 17:01

I have about 50 CGPoint objects that describe something like a \"path\", and I want to add them to an NSArray. It\'s going to be a method that will just return the correspon

4条回答
  •  一个人的身影
    2020-11-30 17:34

    With UIKit Apple added support for CGPoint to NSValue, so you can do:

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

    List as many [NSValue] instances as you have CGPoint, and end the list in nil. All objects in this structure are auto-released.

    On the flip side, when you're pulling the values out of the array:

    NSValue *val = [points objectAtIndex:0];
    CGPoint p = [val CGPointValue];
    

提交回复
热议问题