我似乎想不出这一点,我有一个返回NSArray的objects函数,我确信NSArray中的数据包含CGPoint对象,在这个世界中,我如何将它转换成一个数组
这是函数
+(NSArray *)translatePoints:(NSArray *)points fromView:(UIView *)fromView toView:(UIView *)toView
{
NSMutableArray *translatedPoints = [NSMutableArray new];
// The points are provided in a dictionary with keys X and Y
for (NSDictionary *point in points) {
// Let's turn them into CGPoints
CGPoint pointValue = CGPointMake([point[@"X"] floatValue], [point[@"Y"] floatValue]);
// Now translate from one view to the other
CGPoint translatedPoint = [fromView convertPoint:pointValue toView:toView];
// Box them up and add to the array
[translatedPoints addObject:[NSValue valueWithCGPoint:translatedPoint]];
}
return [translatedPoints copy];
}发布于 2015-01-14 01:29:49
translatesPoints方法返回一个NSArray,其中包含包装CGPoint的NSValues。
let arr:NSArray = [NSValue(CGPoint: CGPointMake(1,2)), NSValue(CGPoint: CGPointMake(3,4))]您可以从这个数组中获取值并对它们调用CGPointValue():
for val in arr as [NSValue] {
let point = val.CGPointValue()
println("CGPoint = (\(point.x), \(point.y))")
}如果需要,可以将整个NSArray转换为CGPoint的Swift数组,如下所示:
let points = (arr as [NSValue]).map({$0.CGPointValue()})现在,points有了[CGPoint]类型。
https://stackoverflow.com/questions/27933893
复制相似问题