如何添加CGPoint对象到NSArray的容易的方法? [英] How can I add CGPoint objects to an NSArray the easy way?
问题描述
我有大约50个CGPoint对象,描述类似路径的东西,我想将它们添加到NSArray。它将是一个方法,将返回给定索引的相应的CGPoint。我不想创建50个变量,如p1 = ...; p2 = ...,等等。有没有一个简单的方法,让我在初始化NSArray与对象时定义这些点?
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 corresponding CGPoint for an given index. I don't want to create 50 variables like p1 = ...; p2 = ..., and so on. Is there an easy way that would let me to define those points "instantly" when initializing the NSArray with objects?
推荐答案
code> UIKit 苹果添加了对CGPoint支持 NSValue
,所以你可以这样做:
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];
列出与您拥有CGPoint一样多的[NSValue]实例,并以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];
这篇关于如何添加CGPoint对象到NSArray的容易的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!